MVC 3, Unity 2 - Per Request Lifetime Manager
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()
开发者_如何学Python 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:
public HomeController(IUnitOfWork unitOfWork)
{
IRepository<Employee> repo = unitOfWork.GetRepository<Employee>();
IRepository<Department> deptrepo = unitOfWork.GetRepository<Department>();
...
}
And if you have not already done so, add:
CIUnity.Container.RegisterControllers();
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.
It looks like you want the UnitOfWorkFactory to return your IUnitOfWork dependencies? Seems like you should be using .RegisterFactory to achieve this. You can register the factory with a ContainerControlledLifetimeManager, and it will behave as a "singleton" without resorting to a static member.
Lifetime management can get a bit tricky with WCF (and probably web). I have been using the code from this article for quite a while. It perfectly maps Unity lifetime to WCF request lifetime... It may give you some ideas about how to best achieve your aim.
http://drewdotnet.blogspot.com/2009/07/unity-lifetime-managers-and-wcf.html
精彩评论