Porting MVC Music Store to RavenSetting 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.
More posts in "Porting MVC Music Store to Raven" series:
- (31 May 2010) StoreManagerController, part 2
- (29 May 2010) StoreManagerController
- (28 May 2010) Porting the checkout process
- (25 May 2010) StoreController
- (24 May 2010) Advanced Migrations
- (23 May 2010) Migrations
- (22 May 2010) Porting the HomeController, the Right Way
- (21 May 2010) Porting the HomeController, the map/reduce way
- (20 May 2010) Data migration
- (19 May 2010) Setting up the application
- (18 May 2010) The data model
Comments
Comment preview