I'm using the Unity MVC3 code at http://unitymvc3.codeplex.com/ to have a NHibernate session per request instance of my IUnitOfWork.
It was working a few weeks ago, I've made some changes, and now my IUnitOfWork appears to have a static lifetime.
From my global.asax.cs:
private static IUnitOfWorkFactory _UoWFactory = new UnitOfWorkFactory(Enums.Databases.MyDBName)
.AddMapping<DepartmentMap>()
.CompleteConfiguration();
/// <summary>
/// Configure Unity Application Block
/// </summary>
private void ConfigureUnity()
{
CIUnity.UnityContainer.Container = new UnityContainer()
.RegisterType(typeof(IRepository<>), typeof(Repository<>))
.RegisterType<IUnitOfWork, UnitOfWork>(Enums.Databases.MyDBName.ToString(), new HierarchicalLifetimeManager(), new InjectionConstructor(Enums.Databases.MyDBName))
.RegisterInstance<IUnitOfWorkFactory>(Enums.Databases.MyDBName.ToString(), _UoWFactory);
DependencyResolver.SetResolver(new UnityDependencyResolver(CIUnity.UnityContainer.Container));
}
ConfigureUnity()
is called from my Application_Start()
Heres me test code from my HomeController constructor:
private IUnitOfWork uow;
private IUnitOfWork uow2;
public HomeController()
{
uow = CIUnity.Container.Resolve<IUnitOfWork>(Enums.Databases.MyDBName.ToString());
IRepository<Employee> repo = uow.GetRepository<Employee>();
uow2 = CIUnity.Container.Resolve<IUnitOfWork>(Enums.Databases.MyDBName.ToString());
IRepository<Department> deptrepo = uow2.GetRepository<Department>();
var q = from a in repo.Find()
select a;
var d = from b in deptrepo.Find()
select b;
}
... the IUnitOfWork is resolved to the same instance, however, on subsequent page requests, a new IUnitOfWork is not created... but the code works, so its somehow registering / pulling the object so that it acts static.