Gravatar Ben Ramey's Blog
Scripture, programming problems, solutions and stories.

NServiceBus with EntityFramework Persisters

Update 1/13/2016

This code (much refactored and improved) is now available as a NuGet package.

Please see documentation and the code over at the GitHub repository.

Summary

NServiceBus is easily configurable to use NHibernate, RavenDB or in-memory persistence. RavenDB is built in and is the default. You can configure it to use in-memory persistence. You can install a NuGet package to use NHibernate which opens the door to many data stores.

So, for a recent project in which we were using NServiceBus, I decided to use NHibernate as the ORM for our simple domain model. I might just be dumb, but I ran into all sorts of problems with NHibernate and MS DTC when I used a remote SQL server. Try as I might, I just couldn’t get NHibernate to work to persist my domain model even though it persisted the NServiceBus stuff just fine.

I decided to switch over to EntityFramework for my domain model. NHibernate and EntityFramework coexisted just fine, but I wanted to use the same ORM for both NServiceBus and my domain model just to make things nice and clean. The problem is, there isn’t a prepackaged solution (none that I found at least) out there to use EntityFramework with NServiceBus. I had to implement my own persistence classes for NServiceBus. As it turns out, that wasn’t too hard. Below is a description of what you will need to do to get EntityFramework working as NServiceBus’ persistence layer. Much of it was modeled directly off of the NHibernate persistence classes found here: https://github.com/Particular/NServiceBus.NHibernate.

Below are all the classes and interfaces I used to implement these persisters. Note that I have EF abstracted away through an IDataContext interface which the persisters use.

  • IPersistSagas
  • ISubscriptionStorage
  • IPersistTimeouts

Actually, this solution isn’t tied specifically to EntityFramework. I have EF abstracted away through an IDataContext interface which the persisters use.

IDataContext

BaseDataContext

EFDataContext

IRepository

EFRepository

Not every method is implemented because I didn’t need them all on this project. Using TDD, I only implemented methods as I needed them.

EFDbContext

This is the project-specific EntityFramework DbContext.

EFSagaPersister

EFSubscriptionPersister

EFTimeoutPersister

TimeoutDataEntity

Subscription

SagaData

Criteria classes

Comments