My unit of work object has 2 dependencies, a connection and a transaction. The transaction needs a connection in order for it to be bound. Unsure of how to handle this situation.
public class UnitOfWork : IUnitOfWork
{
public IDbConnection Connection { get; set; }
public IDbTransaction Transaction { get; set; }
public UnitOfWork(IDbConnection connection, IDbTransaction transaction)
{
this.Connection = connection;
// In order to create the transaction, it needs the passed in IDbConnection.
}
public void Commit()
{
}
public void Rollback()
{
}
}
How would I handle this situation?
Bind<IDbTransaction>().To<SqlTransaction>();
You can bind it like this. In way that IDbTransaction will be tied to IDbConnection.
But I don’t see any reason why should you do this like that. You can simply use
Connection.BeginTransaction()inside yourUnitOfWorkand remove that dependency onIDbTransactionfrom constructor.