Created
March 3, 2012 16:05
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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