Ninject提供商->在提供程序内部获取另一个依赖项 [英] Ninject Providers -> Get another dependency inside the provider
本文介绍了Ninject提供商->在提供程序内部获取另一个依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道这里的最佳做法是什么. 我需要为我的多租户应用程序构造一个DbContext,所以我已经制作了一个像这样的Dependency provider:
I'm wondering what the best practices is here. I need to construct a DbContext for my multi tenanted application, so I have made a Dependency provider like this:
public class TenantContextFactoryProvider : Provider<DbContext>
{
protected override DbContext CreateInstance(IContext context)
{
var tenant = // How to get the tenant through ninject??
return new DbContext(tenant.ConnectionString);
}
}
我需要ninject来解决租户依赖性,但是我不确定该怎么做?
I need ninject to resolve the tenant dependency, but I'm not sure how to do this?
推荐答案
虽然服务定位器确实有效,但构造函数注入是另一种选择.
While service locator certainly works, constructor injection is another choice.
public class TenantContextFactoryProvider : Provider<DbContext>
{
ITenant tenant;
public TenantContextFactoryProvider(ITenant tenant)
{
this.tenant = tenant;
}
protected override DbContext CreateInstance(IContext context)
{
return new DbContext(tenant.ConnectionString);
}
}
这篇关于Ninject提供商->在提供程序内部获取另一个依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文