我正在遵循这个Contoso University MVC NTier University架构。但我不知道如何将其设置为多个数据库上下文。在我的MVC应用程序中,我有两个我经常访问的数据库。
当我在Global.asax中使用UnityContainer时。我注册了两个Dbfactory和Uow,如:
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>>())
我可以完美地查看数据。但是当我查看ServicePRJ时,它的Unitofwork上下文是针对PROJECT2的。请帮助我...我想我需要将上下文传递给单元,但我很难构建我的代码。我是使用这种架构的新编码,使用unitycontainer的新功能,mvc中的新功能。请帮我。
答案 0 :(得分:0)
您对项目2的第二次 IUnitOfWork 注册将覆盖容器中的第一次注册。 您需要为相应的项目创建特定的工作单元接口。
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>>())