I’m using Simple Injector for an IOC in an MVC 3 web application. I am using RavenDB for the data storage. There are several considerations on using RavenDB in an mvc 3 application. I’ve searched some on how to wire-up an IoC to use RavenDB but haven’t found out how to wire-up simple injector to use RavenDB. Can anyone explain how to wire-up simple injector to use RavenDB in an MVC 3 web app?
thanks.
According to the RavenDb tutorial, your application needs exactly one
IDocumentStoreinstance (per database I assume). AIDocumentStoreis thread-safe. It producesIDocumentSessioninstances and they represent a unit of work in RavenDB, and those are not thread-safe. You should therefore not share sessions between threads.How to set up your container for use with RavenDb mainly depends on the application design. The question is: what do you want to inject in to consumers? The
IDocumentStore, or theIDocumentSession?When you go with the
IDocumentStore, your registration could look like this:A consumer might look like this:
Because the
IDocumentStoreis injected, consumers are themselves responsible for managing the session: creation, saving, and disposing. This is very convenient for small applications, or for instance when hiding the RavenDb database behind a repository, where you callsession.SaveChanges()inside therepository.Save(entity)method.However, I found this type of use of a unit of work to be problematic for larger applications. So what you can do instead, is injecting the
IDocumentSessioninto consumers. In that case your registration could look like this:Note that you need the Simple Injector ASP.NET Integration NuGet package (or include the SimpleInjector.Integration.Web.dll to your project, which is included in the default download) to be able to use the
RegisterPerWebRequestextension method.The question now becomes, where to call
session.SaveChanges()?There is a question about registering unit of works per web request, which also addresses the question about
SaveChanges. Please take a good look at this answer: One DbContext per web request…why?. When you replace the wordsDbContextwithIDocumentSessionandDbContextFactorywithIDocumentStore, you will be able read it in the context of RavenDb. Note that perhaps the notion of business transactions or transactions in general are not that important when working with RavenDb, but I honestly don’t know. This is something you will have to find out for yourself.