Skip to content

Instantly share code, notes, and snippets.

@RemiBou
Created September 10, 2012 10:27
Show Gist options
  • Save RemiBou/3690177 to your computer and use it in GitHub Desktop.
Save RemiBou/3690177 to your computer and use it in GitHub Desktop.
Instantiating TransactionScope With Ninject
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<MyDBContext>()
.To<MyDBContext>()
.InRequestScope()
.OnActivation(context => OpenTransaction(kernel))
.OnDeactivation(context => CloseTransaction(kernel));
kernel.Bind<TransactionScope>()
.To<TransactionScope>()
.InRequestScope();
//...
}
private static void RollbackTransaction(IKernel kernel)
{
kernel.Get<TransactionScope>()
.Dispose();
}
private static void OpenTransaction(IKernel kernel)
{
TransactionScope transactionScope = kernel.Get<TransactionScope>();
HttpContext.Current.ApplicationInstance.Error += _applicationInstanceOnError;
}
private static void CloseTransaction(IKernel kernel)
{
var transactionScope = kernel.Get<TransactionScope>();
transactionScope.Complete();
_applicationInstanceOnError = delegate { RollbackTransaction(kernel); };
HttpContext.Current.ApplicationInstance.Error -= _applicationInstanceOnError;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment