NServiceBus with EntityFramework Persisters
This code (much refactored and improved) is now available as a NuGet package.
Please see documentation and the code over at the GitHub repository.
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.
Actually, this solution isn’t tied specifically to EntityFramework. I have EF abstracted away through an IDataContext interface which the persisters use.
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.
This is the project-specific EntityFramework DbContext.