none
IDbConnection, IDbTransaction and Repository RRS feed

  • Question

  • Hello,

    I am back to using IDbConnection and IDbTransaction on my data layer.

    I would like to keep using IoC and Unit Testing on my projects.

    I created 3 classes with thei interfaces: Database, Session and Repository.

    Am I doing this right? And am I implementing Disposing correctly?

    Database class is as follows:

      public class Database : IDatabase {
    
        private IDbConnection _connection;
        public IDbConnection Connection { get { return _connection; } }
    
        public Database (IDbConnection connection) {
          _connection = connection;
        } // Database
    
        public void Open() {
          _connection.Open();
        } // Open
    
        public void Close() {
          _connection.Close();
        } // Close
    
      } // Database

    Session class, which implements IDisposable, is as follows:

      public class Session : ISession {
    
        private IDatabase _database;
        private Boolean _disposed;
        private IDbTransaction _transaction;
        
        public IDbConnection Connection { get { return _database.Connection; } }
        public IDbTransaction Transaction { get { return _transaction; } }
    
        public Session(IDatabase database) {
          _database = database;
          _disposed = false;
        } // Session
    
        public void Begin() {
          _database.Open();
          _transaction = _database.Connection.BeginTransaction(IsolationLevel.ReadCommitted);
        } // Begin
    
        public void Commit() {
          _transaction.Commit();
          _transaction = null;
          _database.Close();
        } // Commit
    
        public void Rollback() {
          _transaction.Rollback();
          _transaction = null;
          _database.Close();
        } // Rollback
    
        public void Dispose() {
          Dispose(true);
          GC.SuppressFinalize(this);
        } // Dispose
    
        // Dispose
        private void Dispose(Boolean disposing) {
          if (!disposing)
            return;
          if (_disposed)
            return;
         
          if (_database.Connection.State != ConnectionState.Closed) {
    
            if (_transaction != null)
              _transaction.Rollback();
    
            _transaction.Dispose();        
            _database.Close();
    
          }
    
          _database.Connection.Dispose();
          _disposed = true;
    
        } // Dispose 
    
      } // Session

    Repository class is:


      public class Repository : IRepository {
    
        private ISession _session;
        public ISession Session { get { return _session; } }
    
        public Repository(ISession session) {
          _session = session;
        } // Repository
    
        public void Open() {
          _session.Begin();
        } // Open
    
        public void Save() {
          _session.Commit();
        } // Save
    
        // Repository methods: Insert, Query, ...
      }

    Could someone, please, advice me on this?

    Thank You,

    Miguel


    Sunday, March 25, 2012 2:07 PM

Answers

  • Small remarks at first glance.

    Since you don't implement a finalizer you don't need to call

    GC.SuppressFinalize(this);

    Nor you should call

     _database.Connection.Dispose();

    instead you should call _database.Dispose() (implement IDisposable at IDatabase level as well) and it should take care of its internals.

    You might also consider transaction isolation level as an argument.

    _disposed is false already (no need to set it explicitly in constructor).


    Miha Markic [MVP C#] http://blog.rthand.com

    Monday, March 26, 2012 2:37 PM