Created
November 29, 2010 13:43
-
-
Save scottlittlewood/719959 to your computer and use it in GitHub Desktop.
Shows a simple implementation of using an OpenRasta Pipeline Contributor to manage nHibernate Sessions on a per request basis
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 NHibernateSessionPipelineContributor : IPipelineContributor | |
{ | |
private readonly IDependencyResolver _resolver; | |
public NHibernateSessionPipelineContributor(IDependencyResolver resolver) | |
{ | |
_resolver = resolver; | |
} | |
public void Initialize(IPipeline pipelineRunner) | |
{ | |
pipelineRunner.Notify(StartSession) | |
.After<KnownStages.IBegin>(); | |
pipelineRunner.Notify(FinishSession) | |
.After<KnownStages.IOperationExecution>() | |
.And | |
.Before<KnownStages.IResponseCoding>(); | |
} | |
private PipelineContinuation StartSession(ICommunicationContext arg) | |
{ | |
var factory = _resolver.Resolve<ISessionFactory>(); | |
if (factory != null) | |
{ | |
var sessionForRequest = factory.OpenSession(); | |
_resolver.AddDependencyInstance(typeof (ISession), sessionForRequest, DependencyLifetime.PerRequest); | |
} | |
return PipelineContinuation.Continue; | |
} | |
private PipelineContinuation FinishSession(ICommunicationContext arg) | |
{ | |
var sessionForRequest = _resolver.Resolve<ISession>(); | |
if (sessionForRequest != null) | |
{ | |
sessionForRequest.Dispose(); | |
} | |
return PipelineContinuation.Continue; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment