Book Image

Learning NServiceBus - Second Edition

By : David Boike
Book Image

Learning NServiceBus - Second Edition

By: David Boike

Overview of this book

Table of Contents (18 chapters)
Learning NServiceBus Second Edition
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Unit of work


Because messages are processed in a pipeline of handlers, you will, at times, have to execute code before the first handler and after the last handler. This is necessary for data stores that follow the unit of work pattern, such as NHibernate or RavenDB, where you need to create a database session before handling a message and then commit or rollback any changes at its completion. One method to accomplish this is by using a unit of work implementation. We will discuss another method when we cover the pipeline later in this chapter.

In order to define a unit of work implementation, you must first implement the NServiceBus.UnitOfWork.IManageUnitsOfWork interface. Here, we have an example that will simply write messages to the console:

public class ConsoleUnitOfWork : IManageUnitsOfWork
{
  public void Begin()
  {
   Console.WriteLine("---Begin message---");
  }

  public void End(Exception ex = null)
  {
   Console.WriteLine("---End message---");
  }
}

The Begin() method is invoked...