Unit of work in NServiceBus 3.0

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. 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. In NServiceBus 3.0 there is a new way to do this, meet the IManageUnitsOfWork interface

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).

Want to see this in action?

My next post will demonstrate how to build a Unit Of Work implementation for RavenDB so stay tuned…

This entry was posted in NServiceBus. Bookmark the permalink. Both comments and trackbacks are currently closed.
  • http://twitter.com/seagile Yves Reynhout

    Might be good if you also explained how this works together with the retry mechanism (I can read the answer between the lines, though).

    • Anonymous

      Good point Yves, I’ll make that a separate post

  • Pingback: A NServiceBus Unit Of Work implementation for RavenDB