.NET Core依赖项注入-许多数据库,一个DbContext [英] .NET Core dependency injection - many databases, one DbContext

查看:475
本文介绍了.NET Core依赖项注入-许多数据库,一个DbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Microsoft.Extensions.DependencyInjection中的DI的ASP.NET Core应用程序.该应用程序通过完全相同的接口连接到数量众多的数据库,比方说其中有100个.我想用相同的DbContext将它们全部覆盖,例如ExternalDbContext.它们之间的唯一区别是它们使用的连接字符串.我需要为给定的请求实例化上下文的正确版本.因此解决方案链将如下所示:

I have an ASP.NET Core application using the DI from Microsoft.Extensions.DependencyInjection. The app connects to a stupid number of databases with an exact same interface, let's say a 100 of them. I want to facade them all with the same DbContext, say ExternalDbContext. The only difference between them is the connection string they're using. I need to instantiate the correct version of the context for a given request. So the chain of resolution would go like this:

  1. 用户发出带有provider = 'ABCD'参数的请求.
  2. 这被映射到具有ISomeService作为依赖项的Controller中的动作.
  3. 该服务具有方法DoStuff(string provider).
  4. 这是关键部分. ISomeService需要对数据层的依赖性,但这不能是对一个注入的ExternalDbContext的硬依赖性,因为必须动态解决.我的想法是要有一个IExternalDbContextFactory,而该IExternalDbContextFactory可以取一个IServiceProvider.该工厂将使用方法GetExternalDbContext(string provider),我将使用注入的服务提供程序解析正确的ExternalDbContext.
  1. User makes a request that has a provider = 'ABCD' parameter.
  2. This is mapped to an Action in a Controller which has an ISomeService as a dependency.
  3. That service has a method DoStuff(string provider).
  4. Here's the crucial part. The ISomeService needs a dependency on the data layer, but this cannot be a hard dependency on one injected ExternalDbContext, since that has to be resolved dynamically. My idea is to have an IExternalDbContextFactory which in turn can take an IServiceProvider. That factory would have a method GetExternalDbContext(string provider) and I would resolve the correct ExternalDbContext using the injected service provider.

要实现此目的,我必须以允许我基于字符串参数从IServiceProvider解析它们的方式注册ExternalDbContexts.出于显而易见的原因,我不想从ExternalDbContext继承100个不同的无用标记类,然后独立注册它们.另外,我还是希望以某种方式使用方便的AddDbContext方法.

To achieve that I would have to register the ExternalDbContexts in a way that would allow me to resolve them from an IServiceProvider based on a string parameter. For obvious reasons I don't want to have a 100 different useless marker classes inheriting from ExternalDbContext and then register them independently. Also I would prefer to use the handy AddDbContext method somehow.

显然,我可以完全构建自己的解析基础结构,但是我宁愿重用现有的解决方案,也不愿花几天时间为该特定用例编写和测试样板.

I obviously could build my own resolution infrastructure entirely, but I'd rather reuse an existing solution than spend days writing and testing boilerplate for this particular use case.

推荐答案

您可以使用AddDbContext重载来管理连接字符串,该重载使您可以访问 :

You can manage connection strings using overload of AddDbContext which provides you access to IServiceProvider:

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddEntityFrameworkSqlServer()
        .AddDbContext<ExternalDbContext>((serviceProvider, options) =>
        {
             var connectionString = serviceProvider.GetService // get service 
             // which can determine connection string
             .GetConnectionString();
              options.UseSqlServer(connectionString);
         });
       }
}

例如,您可以在那里解析IHttpContextAccessor并检查请求参数.

For example you can resolve IHttpContextAccessor there and check the request parameter.

这篇关于.NET Core依赖项注入-许多数据库,一个DbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆