简体   繁体   English

如何使用依赖注入将参数传递给DbContext构造函数

[英]How to pass a parameter to DbContext constructor using Dependency Injection

I'm in the middle of reworking my DbContext to support Multitenancy by requiring a tenantId: 我正在通过要求一个tenantId来重做DbContext以支持多租户的过程:

public AppContext(int tenantId)            
    {
        _tenantId = tenantId;
    }

Before, there was no parameter. 以前,没有参数。

In my service classes, I have the context instantiated with DI: 在我的服务类中,我用DI实例化了上下文:

    private readonly AppContext db;
    private CommService _commService;

    public AdminService(AppContext db, CommService commService)
    {
        this.db = db;
        _commService = commService;
    }

And in my Controllers, the same thing: 在我的控制器中,同样的事情:

    private readonly CommService _commService;
    public AdminController(CommService commService) {
         _commService = commService;
    }

I'm using Unity, but haven't really done much configuration at all - it all just works. 我正在使用Unity,但实际上并没有做太多配置-一切都正常。

I'll be retrieving the tenantId from my Controllers. 我将从控制器中检索tenantId How do I go about passing the tenantId from Controller > Service layer > Constructor? 我该如何从Controller> Service layer> Constructor传递tenantId?

Unity can't pass the tenantId as this is a variable which will depend on the current use for example or any other condition, the tenantId will be determined at runtime, so don't make it injectable. Unity无法传递tenantId因为这是一个变量,例如,这将取决于当前使用情况或任何其他条件, tenantId将在运行时确定,因此不要使其可注入。

Still, you can make a factory for it and injecet this factory. 不过,您可以为其建立工厂并增加该工厂的数量。

ex: 例如:

public Interface ITenantDiscovery
{
  int TenantId{get;}
}

public class UrlTenantDiscovery:ITenantDiscovery
{
 public int TenantId
 {
   get
    {
       var url = -- get current URL, ex: from HttpContext
       var tenant = _context.Tenants.Where(a=>a.Url == url);
       return tenant.Id; -- cache the Id for subsequent calls
    }
 }

In UnityConfig, register the ITenantDiscovery and its implementation UrlTenantDiscovery 在UnityConfig中,注册ITenantDiscovery及其实现UrlTenantDiscovery

Change your AppContext to accept an instance of ITenantDiscovery 更改您的AppContext以接受ITenantDiscovery的实例

public AppContext(ITenantDiscovery tenantDiscovery)            
    {
        _tenantId = tenantDiscovery.TenantId;
    }

That is it. 这就对了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Unity依赖注入时如何将类型作为参数传递给构造函数 - How to pass a type as parameter to a constructor when using Unity dependency injection 如何使用接受 DbContext 作为 asp.net 核心中的参数的依赖注入来传递 class - How to pass class using Dependency Injection that accept DbContext as a parameter in asp.net core Unity(依赖注入):如何将参数传递给RegisterType中的构造函数 - Unity (dependency injection): How to pass in a parameter to the constructor in RegisterType 将 DbContext 与依赖注入一起使用 - Using DbContext with dependency injection 使用DBContext和依赖注入的分歧 - Ramifications of using DBContext with Dependency Injection 依赖注入:使用构造函数注入 - Dependency Injection : Using Constructor Injection 对 DBContext 使用依赖注入时 using 语句应该如何看? - How should using statement look when using Dependency Injection for DBContext? 构造函数的依赖注入参数 - Dependency Injection pass parameters by constructor 使用Unity设置依赖注入以将类的实例传递给构造函数 - Setting up Dependency Injection using Unity to pass instance of class into a constructor 如何使用构造函数中的参数或任何条件实现autofac依赖注入? - how to implement autofac dependency injection with parameter in constructor or with any condition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM