Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save scottlittlewood/719959 to your computer and use it in GitHub Desktop.
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
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