You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The IUnitOfWork interface declares a Begin method, which is not implemented. Instead, UnitOfWork implements a Start method, which should be renamed Begin.
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 EnsureConnection method contains a bug that can produce a runtime exception when the Start method gets called because it tries to get a Transaction instance even if the Connection instance 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();
The
UnitOfWorkimplementation example contains several issues.IUnitOfWorkinterface declares aBeginmethod, which is not implemented. Instead,UnitOfWorkimplements aStartmethod, which should be renamedBegin.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.Instead
EnsureConnectionshould be implemented like that: