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.
I would get rid of the IUnitOfWorkFactory altogether and just inject IUnityOfWork into your controller – providing that you only ever want one unit of work per request which seems logical. You are then utilising the power of the IoC container more and not just using it as a service locator.
Just change your HomeController constructor to:
And if you have not already done so, add:
before you call DependencyResolver.SetResolver
When the MVC core tries to instantiate your controller, it will use the Unity.MVC3 resolver which will automatically satisfy any dependencies which in this case means the unitOfWork.
The Unity.MVC3 resolver will also automatically take care of disposing your unit of work at the end of the request.