【发布时间】:2015-03-20 19:19:24
【问题描述】:
我有一个多租户应用程序。我必须用 Autofac 解析一个类型。我能做到……这并不难。这里我是怎么做的:
第一个解决方案
var tenantIdentifier = new TenantIdentificationStrategy();
var mtc = new MultitenantContainer(tenantIdentifier, container);
mtc.ConfigureTenant("1", t =>
{
t.RegisterType<MultiTenant.ModuleModule1>()
.As<MultiTenant.IModule>();
t.RegisterType<MultiTenant.Controllers.Tenant.Tenant1Controller>()
.As<MultiTenant.Controllers.CommonController>()
.PropertiesAutowired();
});
mtc.ConfigureTenant("2", t =>
{
t.RegisterType<MultiTenant.ModuleModule2>()
.As<MultiTenant.IModule>();
t.RegisterType<MultiTenant.Controllers.Tenant.Tenant2Controller>()
.As<MultiTenant.Controllers.CommonController>()
.PropertiesAutowired();
});
嗯...它工作得很好...它按我的预期工作 但我不喜欢我必须添加具体的注册类。这意味着我必须在项目中添加一个引用......例如,如果我有 10 个不同的租户,我必须向我的项目添加 10 个引用。 但我不太喜欢这种情况。
第二个解决方案
我试过这样解决:
mtc.ConfigureTenant("1", t =>
{
t.RegisterType<MultiTenant.Controllers.Tenant.Tenant1Controller>()
.As<MultiTenant.Controllers.CommonController>()
.PropertiesAutowired();
});
mtc.ConfigureTenant("2", t =>
{
t.RegisterType<MultiTenant.Controllers.Tenant.Tenant2Controller>()
.As<MultiTenant.Controllers.CommonController>()
.PropertiesAutowired();
});
在网络配置中:
<autofac>
<components>
<component type="MultiTenant.Module1, MultiTenant.Module" service="MultiTenant.IModule, MultiTenant.IModule"></component>
<component type="MultiTenant.Module2, MultiTenant.Module" service="MultiTenant.IModule, MultiTenant.IModule"></component>
</components>
</autofac>
此解决方案“有效”,但我没有预期的结果,因为 Module1 和 Module2 都为两个租户解析。
我该如何解决这个问题?
【问题讨论】:
标签: c# dependency-injection autofac multi-tenant