Created
September 10, 2012 10:27
-
-
Save RemiBou/3690177 to your computer and use it in GitHub Desktop.
Instantiating TransactionScope With Ninject
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
/// <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