注入存储库的动态连接字符串 [英] dynamic connection string for injected repository
问题描述
我正在构建一个多租户(每个租户数据库)的应用程序.
I'm building a multi tenant (db per tenant) application.
- 应用程序-.net MVC4
- 数据层实体框架代码第一个存储库
- 存储库通过结构图注入到应用程序中
结构图配置:
var connectionString = ConfigurationManager.ConnectionStrings["AccessControlDB"].ToString();
ObjectFactory.Initialize(x =>
{
x.For<IAccessControlContext>().Use<AccessControlContext>().Ctor<string>("connectionString").Is(connectionString);
x.For<IGenericRepository<Identities>>().Use<GenericRepository<IAccessControlContext, Identities>>();
x.Scan(scan =>
{
scan.AssembliesFromApplicationBaseDirectory();
scan.ExcludeNamespace("StructureMap");
scan.WithDefaultConventions();
});
});
提出了由数据库隔离每个租户的新要求,因此拥有tenantID不再足够.
A new requirement for having each tenant isolated by database has come up, so having tenantIDs is no longer sufficient.
到目前为止,我已经拥有一个元数据库来存储构造连接字符串所需的数据,但是我对如何将连接字符串传递到存储库感到困惑.
起初,我以为我可以将连接字符串作为属性公开,但是在结构映射实例化上下文之后,无法更改连接字符串.
At first, I thought I could just expose the connection string as a property, but there is no way to change the connection string after the context has already been instantiated by structuremap.
推荐答案
代替将连接字符串传递到存储库,请执行以下操作:
Instead of passing a connection string to the repositories, do the following:
- 将
IContextFactory
注入存储库.此上下文工厂可以创建新的DbContext
实例. - 将
ITenantProvider
注入连接工厂.
- Inject an
IContextFactory
into the repositories. This context factory can create newDbContext
instances. - Inject an
ITenantProvider
into the connection factory.
将上下文工厂注入存储库具有以下优点:
Injecting a context factory into the repositories has the following advantages:
- 使容器的配置变得相当容易,因为您消除了
string
值带来的歧义,从而避免了将连接字符串注入许多类的麻烦. - 由于上下文工厂现在负责创建正确的连接字符串(基于从
ITenantProvider
实现中获得的租户信息),因此它可以隐藏在抽象背后解析连接字符串的复杂性. li>
- 通过从工厂返回新的
DbContext
实例而不是返回连接字符串,可以隐藏DbContext
的创建.这使您的存储库更容易,并且以后更容易更改它.
- Make it considerably easier to configure the container, because you remove the ambiguity that
string
values bring and this prevents you from having to inject that connection string into many classes. - It allows you to hide the complexity of resolving the connection string behind an abstraction, since the context factory is now responsible of creating the right connection string (based on the tenant information it got from the
ITenantProvider
implementation). - By returning new
DbContext
instances from the factory instead of returning connection strings, you hide the creation of theDbContext
. This makes your repository easier and makes it easier to change it later on.
请注意,我只是在这里猜测存储库的工作方式,因此使用IContextFactory
可能不是最佳选择.但是,通过防止直接注入连接字符串并将其隐藏在抽象后面(甚至比返回该连接字符串还要多),您可以大大简化DI配置和应用程序代码,并进行维护和测试.
Note that I'm just guessing here how your repository works so the use of an IContextFactory
might not be the best option. But by preventing to inject the connection string directly and hiding it behind an abstraction (that even does a bit more than just returning that connection string), you can make both your DI configuration and application code considerably easier and maintain and test.
这篇关于注入存储库的动态连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!