Skip to content

Instantly share code, notes, and snippets.

@ArnisL
Created August 20, 2010 09:22
Show Gist options
  • Save ArnisL/539955 to your computer and use it in GitHub Desktop.
Save ArnisL/539955 to your computer and use it in GitHub Desktop.
namespace Interreg.Web.Bootstrap{
using System;
using Config;
using Microsoft.Practices.ServiceLocation;
using MvcExtensions;
using NHibernate;
using NHibernate.Context;
using StructureMap;
public class NHibernateTask:BootstrapperTask{
private readonly IContainer _container;
public NHibernateTask(IContainer container){
if(container==null) throw new ArgumentNullException();
_container=container;
}
public override TaskContinuation Execute(){
NHibernateBootstrap.SpinIt(_container);
return TaskContinuation.Continue;
}
}
public class NHibernatePerRequestTask:PerRequestTask{
private readonly ISessionFactory _factory;
private readonly IServiceLocator _locator;
public NHibernatePerRequestTask(IServiceLocator locator){
if(locator==null) throw new ArgumentNullException();
_locator=locator;
_factory=_locator.GetInstance<ISessionFactory>();
}
public override TaskContinuation Execute(){
CurrentSessionContext.Bind(_factory.OpenSession());
return TaskContinuation.Continue;
}
protected override void DisposeCore(){
_factory.GetCurrentSession().Flush();
CurrentSessionContext.Unbind(_factory);
base.DisposeCore();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment