Skip to content

Instantly share code, notes, and snippets.

@Rookian
Created June 1, 2012 11:53
Show Gist options
  • Save Rookian/2851559 to your computer and use it in GitHub Desktop.
Save Rookian/2851559 to your computer and use it in GitHub Desktop.
DependencyInjection StructureMap HttpModule
/// <summary>
/// HttpModule is the better choice for session management, because when more than one controller is involved within one webrequest,
/// the session will be disposed by one controller and the other controller can not reuse the session, because it is already disposed
/// </summary>
public class UnitOfWorkHttpModule : IHttpModule
{
/// <summary>
/// Should be refactored to use constructor injection
/// http://bugsquash.blogspot.de/2009/11/windsor-managed-httpmodules.html
/// </summary>
public static Func<IUnitOfWork> GetUnitOfWork = () => { throw new NotImplementedException("GetUnitOfWork is not configured."); };
public void Init(HttpApplication context)
{
context.BeginRequest += BeginRequest;
context.EndRequest += EndRequest;
}
static void BeginRequest(object sender, EventArgs e)
{
var unitOfWork = GetUnitOfWork();
unitOfWork.Begin();
}
static void EndRequest(object sender, EventArgs e)
{
var unitOfWork = GetUnitOfWork();
try
{
unitOfWork.Commit();
}
catch
{
unitOfWork.RollBack();
throw;
}
finally
{
unitOfWork.Dispose();
}
}
public void Dispose(){}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment