Thursday 20 February 2014

How to follow best practices of NHibernate with WCF integration?

If you work with NHibernate, you are probably familiar with its best practices. When working with WCF we need to maintain UnitOfWork and transactions. There is a library that makes this integration seamless: NHJump by James Lanng and Richard Penrose.

Marking your service class with an attribute will do the magic:
  • will create a unit of work per request
  • will open a transaction
  • will commit it at the end of the request or rollback it if an exception was thrown
 [ServiceContract] 
public interface IPersistenceService  {  ...  }  
[UnitOfWork]
public class PersistenceService : IPersistenceService  {  ...  }

Sounds nice? It has also ASP.NET MVC integration.

Samples can be found on SVN repository:

Available on nuget:

PM> Install-Package NhJump.Persistence