What is the best way to override ISession.BeginTranscation and ISession.Transaction?
I would like to wrap NHibernate's ITransaction with Rx to observe when transactions rollback and commit.
public interface IObservableTransaction : ITransaction
{
IDisposable SubscribeToCommit(Action<ITransaction> actionOnCommit);
IDisposable SubscribeToRollback(Action<ITransaction> actionOnRollBack);
}
public class ObservableTransaction : IObservableTransaction
{
private readonly ITransaction _transaction;
private readonly Subject<ITransaction> _commitSubject = new Subject<ITransaction>();
private readonly Subject<ITransaction> _rollbackSubject = new Subject<ITransaction>();
public ObservableTransaction(ITransaction transaction)
{
_transaction = transaction;
}
public void Dispose()
{
_transaction.Dispose();
_commitSubject.Dispose();
}
public void Begin()
{
_transaction.Begin();
}
public void Begin(IsolationLevel isolationLevel)
{
_transaction.Begin(isolationLevel);
}
public IDisposable SubscribeToCommit(Action<ITransaction> actionOnCommit)
{
return _commitSubject.Subscribe(actionOnCommit);
}
public void Commit()
{
_transaction.Commit();
_commitSubject.OnNext(this);
_commitSubject.OnCompleted();
_rollbackSubject.OnCompleted();
}
public IDisposable SubscribeToRollback(Action<ITransaction> actionOnRollBack)
{
return _rollbackSubject.Subscribe(actionOnRollBack);
}
public void Rollback()
{
_transaction.Rollback();
_rollbackSubject.OnNext(this);
_commitSubject.OnCompleted();
_rollbackSubject.OnCompleted();
}
public void Enlist(IDbCommand command)
{
_transaction.Enlist(command);
}
public void RegisterSynchronization(ISynchronization synchronization)
{
_transaction.RegisterSynchronization(synchronization);
}
public bool IsActive
{
get { return _transaction.IsActive; }
}
public bool WasRolledBack
{
get { return _transaction.WasRolledBack; }
}
public bool WasCommitted
{
get { return _transaction.WasCommitted; }
}
}