Is the code snippet below a good implementation of the session-per-request pattern in an ASP.NET application? If so, why do people bother creating a custom IHttpModule for this task?
The SessionManager is a custom helper class that stores session factories in a thread-safe singleton.
Code:
public class Global : System.Web.HttpApplication
{
public static ISession CurrentSession { get; private set; }
protected void Application_Start(object sender, EventArgs e)
{
// Initialize default session factory
SessionManager.ConfigureFactory("ConnectStringKey", "AssemblyName");
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
CurrentSession = SessionManager.OpenSession();
}
protected void Application_EndRequest(object sender, EventArgs e)
{
if (CurrentSession != null && CurrentSession.IsOpen)
{
CurrentSession.Flush();
CurrentSession.Close();
}
CurrentSession = null;
}
etc.