I am following this Contoso University MVC NTier University architecture. But I don't know how to set it to multiple database context. In my MVC application, I have two database that I usually access.
When I use the UnityContainer in Global.asax. I Register two Dbfactory and Uow like :
IUnityContainer oContainer = new UnityContainer()
// ***** PROJECT *****
.RegisterType<IProjectDBFactory, ProjectDBFactory>(new HttpContextLifetimeManager<IProjectDBFactory>())
.RegisterType<IUnitOfWork, ProjectUow>(new HttpContextLifetimeManager<IUnitOfWork>())
.RegisterType<IRepoPRJTABLE, RepoPRJTABLE>(new HttpContextLifetimeManager<IRepoPRJTABLE>())
.RegisterType<IServiceRepository<PRJTABLE>, ServicePRJTABLE>(new HttpContextLifetimeManager<IServiceRepository<PRJTABLE>>())
// ***** PROJECT2 *****
.RegisterType<IProject2DBFactory, Project2WebDBFactory>(new HttpContextLifetimeManager<IProject2DBFactory>())
.RegisterType<IUnitOfWork, Project2Uow>(new HttpContextLifetimeManager<IUnitOfWork>())
.RegisterType<IRepoPRJ2TABLE, RepoPRJ2TABLE>(new HttpContextLifetimeManager<IRepoPRJ2TABLE>())
.RegisterType<IServiceRepository<PRJ2TABLE>, ServicePRJ2TABLE>(new HttpContextLifetimeManager<IServiceRepository<PRJ2TABLE>>())
I can view the data perfectly. But when I look into the ServicePRJ, it's Unitofwork context is the for the PROJECT2. Please help me... I think I need to pass the context to the unitwork but I am having hard time constructing my code. I am new in coding with such architecture, new in using unitycontainer, new in mvc. Please help me.
Your second registration of IUnitOfWork for project 2 is overriding the first registration in the container. You need to make specific unit of work interfaces for the respective projects.
interface IProjectUnitOfWork : IUnitOfWork {}
interface IProject2UnitOfWork : IUnitOfWork {}
....
IUnityContainer oContainer = new UnityContainer()
// ***** PROJECT *****
.RegisterType<IProjectDBFactory, ProjectDBFactory>(new HttpContextLifetimeManager<IProjectDBFactory>())
.RegisterType<IProjectUnitOfWork, ProjectUow>(new HttpContextLifetimeManager<IProjectUnitOfWork >())
.RegisterType<IRepoPRJTABLE, RepoPRJTABLE>(new HttpContextLifetimeManager<IRepoPRJTABLE>())
.RegisterType<IServiceRepository<PRJTABLE>, ServicePRJTABLE>(new HttpContextLifetimeManager<IServiceRepository<PRJTABLE>>())
// ***** PROJECT2 *****
.RegisterType<IProject2DBFactory, Project2WebDBFactory>(new HttpContextLifetimeManager<IProject2DBFactory>())
.RegisterType<IProject2UnitOfWork, Project2Uow>(new HttpContextLifetimeManager<IProject2UnitOfWork >())
.RegisterType<IRepoPRJ2TABLE, RepoPRJ2TABLE>(new HttpContextLifetimeManager<IRepoPRJ2TABLE>())
.RegisterType<IServiceRepository<PRJ2TABLE>, ServicePRJ2TABLE>(new HttpContextLifetimeManager<IServiceRepository<PRJ2TABLE>>())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With