Porting MVC Music Store to Raven: Setting up the application
Just a few words about the way that I setup Raven to be used in the MVC Music Store application before we get to the actual code.
- The model is (intentionally) very close to the one used by NHibernate. We initialize the document store in the application start.
- We then open/close the session on request boundary, and create a way to access the current session.
- If the application supported a container, I would make sure that the controllers got the session instance through that, but it doesn’t, so I just used static gateway.
- If you don’t like, feel free to submit a patch.
public class MvcApplication : System.Web.HttpApplication { private const string RavenSessionKey = "Raven.Session"; private static DocumentStore _documentStore; protected void Application_Start() { _documentStore = new DocumentStore { Url = "http://localhost:8080/" };
_documentStore.Initialise(); AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); } public MvcApplication() { BeginRequest += (sender, args) => HttpContext.Current.Items[RavenSessionKey] = _documentStore.OpenSession(); EndRequest += (o, eventArgs) => { var disposable = HttpContext.Current.Items[RavenSessionKey] as IDisposable; if (disposable != null) disposable.Dispose(); }; } public static IDocumentSession CurrentSession { get { return (IDocumentSession) HttpContext.Current.Items[RavenSessionKey]; } } }
This is pretty much it, as far as Raven’s initialization is concerned.
Comments
No comments posted yet.