PC Review


Reply
Thread Tools Rate Thread

IDbConnection, IDbTransaction and Repository

 
 
Shapper
Guest
Posts: n/a
 
      25th Mar 2012
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

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
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      26th Mar 2012
On 3/25/2012 10:09 AM, Shapper wrote:
> 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
>
> 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?


I find it very difficult to see what you achieve with
all this code that you would not achieve with 1
interface plus 1 class implementing your interface
and IDisposable.

Arne

 
Reply With Quote
 
 
 
 
Shapper
Guest
Posts: n/a
 
      26th Mar 2012
On Monday, March 26, 2012 1:57:14 AM UTC+1, Arne Vajh°j wrote:
> On 3/25/2012 10:09 AM, Shapper wrote:
> > 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
> >
> > 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?

>
> I find it very difficult to see what you achieve with
> all this code that you would not achieve with 1
> interface plus 1 class implementing your interface
> and IDisposable.
>
> Arne


Could you, please, give me an example?
I think you mean to have only the repository, correct?
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      27th Mar 2012
On 3/26/2012 6:01 AM, Shapper wrote:
> On Monday, March 26, 2012 1:57:14 AM UTC+1, Arne Vajh°j wrote:
>> On 3/25/2012 10:09 AM, Shapper wrote:
>>> 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
>>>
>>> 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?

>>
>> I find it very difficult to see what you achieve with
>> all this code that you would not achieve with 1
>> interface plus 1 class implementing your interface
>> and IDisposable.

>
> Could you, please, give me an example?
> I think you mean to have only the repository, correct?


Yes.

Have Repository wrap IDbConnection instead of
Repository wrapping Session wrapping Database
wrapping IDbConnection.

I can not seen any benefits by having 3 classes.

Arne

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Failed copying file '811114_asp51.dll' into QFE repository folder. repository folder {41B0394B-74D8-4CFD-86C0-BDCE45CE4728} does not exist. Zhang Hui Windows XP Embedded 1 4th Mar 2005 05:30 AM
SqlDataAdapter & IDbTransaction =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= Microsoft ADO .NET 3 3rd Feb 2005 02:49 PM
IDbTransaction spanning several methods Ola Strandberg Microsoft ADO .NET 2 28th May 2004 09:48 AM
Re-install woes: CBrowserCtrl load fail and Repository read-only Mark K Vallevand Windows XP Embedded 4 1st Mar 2004 08:59 PM
IDbTransaction.Dispose() behavior Alexander Myachin Microsoft ADO .NET 1 17th Sep 2003 12:43 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:14 PM.