Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Database Context

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.

  • Project.Common (this is where put the common infrastructure like the IUnitOfWork, IRepository)
  • Project.Data
  • Project.Models
  • Project.Service
  • Project2.Data
  • Project2.Models
  • Project2.Service
  • Project.Web

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.

like image 827
babyG Avatar asked Dec 01 '25 02:12

babyG


1 Answers

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>>())
like image 80
Nkosi Avatar answered Dec 02 '25 22:12

Nkosi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!