Apostol Apostolov

Practical thoughts about software

Managing RavenDB session in .NET web application

When using RavenDB in the context of a web application we should not open RavenDB's session for every operation of the database. Session should rather be opened for every Request/Response cycle(every button click for example).

A way to do that is to open RavenDB's DocumentStore in Application_Start, open a new session in Application_BeginRequest and close the session at Application_EndRequest events of your application’s Global.asax file:

In the case of ASP.NET MVC we are going to have BaseController - the one that all controllers inherit from. In the Base Controller we have:

When we need to do something and use the session in a controller that inherits from BaseController:

This way of defining and using RavenDB’s session makes the use of the session decoupled from the opening and closing of the session and also decoupled from the Document Store. The approach is very good for scenarios where we want to use the same controllers outside the scope of the current web application – for example in a Unit Testing project.

Tags:

Posted By: Apostol Apostolov

Published at

Originally posted at

Comments

No comments posted yet.