HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

SQLite helper class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
helpersqliteclass

Problem

I have recently implemented my SQLite helper class that supports SQLite in a memory class to be opened to not to be lost. Please review it and tell me if there is a coding problem and tell me what to do to prevent\fix it.

```
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;

namespace SQLite
{
public class SqLiteDatabase : IDisposable
{
private readonly SQLiteConnection _dbConnection;

///
/// Default Constructor for SQLiteDatabase Class.
///
public SqLiteDatabase()
{
_dbConnection = new SQLiteConnection("Data Source=default.s3db");
}

///
/// Single Param Constructor to specify the datasource.
///
/// The data source. Use ':memory:' for in memory database.
public SqLiteDatabase(String datasource)
{
_dbConnection = new SQLiteConnection(string.Format("Data Source={0}", datasource));
}

///
/// Single Param Constructor for specifying advanced connection options.
///
/// A dictionary containing all desired options and their values.
public SqLiteDatabase(Dictionary connectionOpts)
{
String str = connectionOpts.Aggregate("",
(current, row) =>
current + String.Format("{0}={1}; ", row.Key, row.Value));
str = str.Trim().Substring(0, str.Length - 1);
_dbConnection = new SQLiteConnection(str);
}

#region IDisposable Members

public void Dispose()
{
if (_dbConnection != null)
_dbConnection.Dispose();

GC.Collect();
GC.SuppressFinalize(this);
}

#endregion

public bool OpenConnection()
{
try

Solution

In Dispose():

  • You are calling GC.Collect() which isn't necessarily need unless it is very important to collect all inaccessible memory when disposing.



  • GC.SuppressFinalize(this) isn't need because the class don't have a finalizer/deconstructor



In CloseConnection() you are always returning false. Why not make it void?

You are handling a lot of exceptions in this "framework", why not let the "client" handle the errors? It will make you code more maintainable and easy to read. It would remove a bunch of return and try..catch statements (as discussed in the comments), and allows the "client" to handle the exceptions differently if needed.

Context

StackExchange Code Review Q#23284, answer score: 3

Revisions (0)

No revisions yet.