Single App consuming multiple IoC registries and has conflicts

筅森魡賤 提交于 2019-12-13 01:42:40

问题


I have a single application that talks to separate databases through two individual UnitOfWork classes. I am using StructureMap to build my classes. Individually, each one builds and runs correctly.

However, once I consume both container registries within my "Web Application", I get conflicts. Both registries reference the same DbContext (base) class from their common framework. However, each registry has its own concrete DbContext class (that inherits from DbContext).

The problem is...

  1. The moment I consume both registries, last in "wins"...and the wrong CONCRETE Dbcontext class is applied (into the first registry).
  2. Multiple instances of DbContext are applied to the Repository references.

Q: How do I ensure the right CONCRETE DbContext is applied across my registry?
Q: How do I ensure a SINGLE INSTANCE of the DbContext across my registry?

I have tried various methods without success...thanks for the help.

For Example...

REGISTERY 'ONE' CONTAINS:

For<DbContext>().Use<BBLDataContext>();
For<IUnitOfWork>().Use<BBLUnitOfWork>();

For(typeof(ICompositeRepository<>)).Use(typeof(DbRepository<>)).Ctor<DbContext>().Is<BBLDataContext>();
For(typeof(IRepository<>)).Use(typeof(DbRepository<>)).Ctor<DbContext>().Is<BBLDataContext>();

REGISTERY 'TWO' CONTAINS:

For<DbContext>().Use<WsDataContext>();
For<IUnitOfWork>().Use<WsUnitOfWork>();

For(typeof(ICompositeRepository<>)).Use(typeof(DbRepository<>)).Ctor<DbContext>().Is<WsDataContext>();
For(typeof(IRepository<>)).Use(typeof(DbRepository<>)).Ctor<DbContext>().Is<WsDataContext>();

来源:https://stackoverflow.com/questions/52996330/single-app-consuming-multiple-ioc-registries-and-has-conflicts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!