Skip to content

Instantly share code, notes, and snippets.

@asapostolov
Created March 3, 2012 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asapostolov/1966819 to your computer and use it in GitHub Desktop.
Save asapostolov/1966819 to your computer and use it in GitHub Desktop.
Initalizes and manages RavenDB Document Store and Session. Initalize() method must be called in Application_Start event to initialize the store and optionally - SessionSaveAndClose() on Application_PostRequestHandlerExecute to save the changes.
public class DocumentManager
{
public static readonly string SessionKey = "current.session";
public static IDocumentStore CurrentDocumentStore { get; private set; }
public static IDocumentSession CurrentSession
{
get
{
IDocumentSession session = null;
if (HttpContext.Current != null)
{
session = HttpContext.Current.Items[SessionKey] as IDocumentSession;
if (session == null)
{
session = CurrentDocumentStore.OpenSession();
HttpContext.Current.Items.Add(SessionKey, session);
}
}
return session;
}
private set
{
HttpContext.Current.Items.Add(SessionKey, value);
}
}
private static bool IsSessionCreated
{
get
{
return HttpContext.Current.Items.Contains(SessionKey);
}
}
public static void Initalize()
{
CurrentDocumentStore = new DocumentStore() { ConnectionStringName="RavenDb" };
CurrentDocumentStore.Initialize();
Raven.Client.MvcIntegration.RavenProfiler.InitializeFor(CurrentDocumentStore);
}
public static void SessionSaveAndClose()
{
if (IsSessionCreated)
{
CurrentSession.SaveChanges();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment