我已成立了一个新的MVC项目一个DataContext,而我使用structuremap来处理我的DI/IoC的。我正在使用实体框架和缓存存储库,存储库类和EF数据上下文。有条件选择具有structuremap
所以,(简化的)我有一个IRepository其由混凝土实现 - CacheRepository和EFRepository。 EFRepository取决于DataContext的(的DbContext)和CacheRepository取决于IRepository(作为持久性存储库回退),并ICachingService(所以我可以选择我的缓存提供商),然后IService通过服务其取决于许多库来实现的。
我的问题是,根据该网站的区域记录到(例如管理员有一个单独的登录数据库 - 这是一个原因,我不会进入这里所做的)我的登录信息数据库是不同的。我有一个包含所有用户表的基础数据上下文,并从两个主要的数据上下文继承;所以根据用户尝试登录的站点区域,决定检查哪个数据库的细节 - 所以我需要能够根据控制器选择datacontext或连接字符串。
有人能为我提供一些指导如何实现的datacontext该条件选择/ ConnectionString的基于控制器上所用structuremap实例?
我的IoC注册表目前看起来是这样的(引用datacontexts之一):
For<BaseDataContext>().LifecycleIs<HttpContextLifecycle>()
.Use(x => new AdminDataContext());
//REPOSITORIES
For<IDAL.Repositories.Users.IApplicationUserRepository>()
.Use<DAL.Repositories.Users.EFApplicationUserRepository>();
For<IDAL.Repositories.Users.IUserLoginAttemptRepository>()
.Use<DAL.Repositories.Users.EFUserLoginAttemptRepository>();
For<IDAL.Repositories.Users.IUserRoleRepository>()
.Use<DAL.Repositories.Users.CacheRoleRepository>()
.Ctor<IDAL.Repositories.Users.IUserRoleRepository>()
.Is<DAL.Repositories.Users.EFUserRoleRepository>()
.Ctor<IDAL.Caching.IGenericCachingService<Common.Objects.Data.Users.UserRole>>()
.Is<DAL.Caching.HttpContextGenericCachingService<Common.Objects.Data.Users.UserRole>>();
//SERVICES
For<IBLL.Services.IUserService>()
.Use<BLL.Services.UserService>();
这有什么好运气? – 2014-12-22 19:13:21
@ Javid_p84,到目前为止没有任何想法?干杯。 – Carl 2014-12-29 09:51:11
不是真的。我添加了几个虚拟接口来克服它。有趣的是,迄今为止还没有解决方案(或者至少我们不知道)。 – 2014-12-30 17:14:35