When using a framework like NServiceBus there is usually a need to create your own unit of work in order to avoid having to repeat code over an over again in your message handlers.
Examples would be committing NHibernate transactions, calling SaveChanges on the RavenDB session, etc etc.
In NServiceBus 2.6 your only hook into the message pipeline was the message modules, you can read about them in Jonathan Oliver's post.
This had some quirks since the HandleEndMessage method of the message module will be invoked regardless of the outcome and made it hard to decide if a commit or rollback should be performed.
Starting from NServiceBus version 3.0 there is a new way to do this, meet the IManageUnitsOfWork interface
public interface IManageUnitsOfWork { /// <summary> /// Called before all message handlers and modules /// </summary> void Begin(); /// <summary> /// Called after all message handlers and modules /// </summary> void End(Exception ex = null); }
The semantics are that that Begin() will be called when the transport messages enters the pipeline, remember that a transport message can consist of multiple application messages. This allows you to do any setup that is required.
The End() method is called when the processing is complete and if there was an exception that exception will be passed into the method.
This gives you a way to perform different actions depending on the outcome of the message(s).
