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 oneIDocumentStore
instance (per database I assume). A IDocumentStore
is thread-safe. It produces IDocumentSession
instances 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 the IDocumentSession
?
When you go with the IDocumentStore
, your registration could look like this:
// Composition Root
IDocumentStore store = new DocumentStore
{
ConnectionStringName = "http://localhost:8080"
};
store.Initialize();
container.RegisterSingle<IDocumentStore>(store);
A consumer might look like this:
public class ProcessLocationCommandHandler
: ICommandHandler<ProcessLocationCommand>
{
private readonly IDocumentStore store;
public ProcessLocationCommandHandler(IDocumentStore store)
{
this.store = store;
}
public void Handle(ProcessLocationCommand command)
{
using (var session = this.store.OpenSession())
{
session.Store(command.Location);
session.SaveChanges();
}
}
}
Because the IDocumentStore
is 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 call session.SaveChanges()
inside the repository.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 IDocumentSession
into consumers. In that case your registration could look like this:
IDocumentStore store = new DocumentStore
{
ConnectionStringName = "http://localhost:8080"
};
store.Initialize();
// Register the IDocumentSession per web request
// (will automatically be disposed when the request ends).
container.RegisterPerWebRequest<IDocumentSession>(
() => store.OpenSession());
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 RegisterPerWebRequest
extension 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 words DbContext
with IDocumentSession
and DbContextFactory
with IDocumentStore
, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With