IDbConnection, IDbTransaction and Repository


S

Shapper

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
 
Ad

Advertisements

A

Arne Vajhøj

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
 
S

Shapper

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?
 
Ad

Advertisements

A

Arne Vajhøj

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top