-
Notifications
You must be signed in to change notification settings - Fork 8
Bug: UnitOfWork example contains errors #43
Copy link
Copy link
Open
Labels
bugSomething isn't workingSomething isn't working
Description
The UnitOfWork implementation example contains several issues.
- The
IUnitOfWorkinterface declares aBeginmethod, which is not implemented. Instead,UnitOfWorkimplements aStartmethod, which should be renamedBegin.
public interface IUnitOfWork<TDbConnection>
{
TDbConnection Connection { get; }
DbTransaction Transaction { get ; }
void Begin();
void Rollback();
void Commit();
}
public void Start()
{
if (transaction != null)
{
throw new InvalidOperationException("Cannot start a new transaction while the existing other one is still open.");
}
var connection = EnsureConnection();
transaction = connection.BeginTransaction();
}
- The
EnsureConnectionmethod contains a bug that can produce a runtime exception when theStartmethod gets called because it tries to get aTransactioninstance even if theConnectioninstance is not opened yet.
private SqlConnection EnsureConnection() =>
connection ?? connection = new SqlConnection(settings.ConnectionString);
Instead EnsureConnection should be implemented like that:
private SqlConnection EnsureConnection() =>
connection ??= (SqlConnection) new SqlConnection(settings.ConnectionString).EnsureOpen();
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working