简体   繁体   English

具有多个数据库的存储库模式

[英]Repository pattern with multiple databases

I am using repository pattern on EF Core and Autofac in a windows service. 我在Windows服务的EF Core和Autofac上使用存储库模式。

I have a service that needs to connect with the some dozen databases which have the same schema (same dbcontext) but only different data. 我有一项服务,需要与数十个具有相同模式(相同dbcontext)但只有不同数据的数据库连接。 How can I achieve this in my service using Autofac? 如何使用Autofac在我的服务中实现这一目标? Belo 贝洛

public class ReportRepository : IReportRepository
    {
        private readonly ReportDbContext dbContext;

        public ReportRepository(ReportDbContext dbContext)
        {
            this.dbContext = dbContext
        }

        public SomeModel GetData()
        {
            return dbContext.SalesData;
        }
    }

    public class ReportService : IReportService 
    {
        private readonly IReportRepository reportRepositoryEUServer;

        public ReportService(IReportRepository reportRepositoryEUServer)
        {
            this.reportRepositoryEUServer = reportRepositoryEUServer
        }

        public SomeModelDto GenerateReport()
        {
            var euData =  reportRepositoryEUServer.GetData();
            // I need to call other servers (e.g LATAM) here and get the data and aggregate them with euData
        }       
    }

Create base context including all settings, dbsets etc: 创建基本上下文,包括所有设置,dbset等:

public abstract class BaseContext : DbContext
{
    public BaseContext(DbContextOptions options)
    : base(options)
    { }
    public DbSet<object> FirstSet { get; set; }
    ...
}

inherit from BaseContext for both DBs BaseContext继承两个数据库

public class LATAMContext : BaseContext
{
    public LATAMContext(DbContextOptions<LATAMContext> options) : base(options)
    {

    }
}

public class EUContext : BaseContext
{
    public EUContext(DbContextOptions<EUContext> options) : base(options)
    {

    }
}

and register both in Startup.cs 并在Startup.cs注册

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<LATAMContext>(options => options.UseSqlServer(Configuration.GetConnectionString("LATAMConnectionString")));
    services.AddDbContext<EUContext>(options => options.UseSqlServer(Configuration.GetConnectionString("EUConnectionString")));

    // Autofac
    var builder = new ContainerBuilder();

    // needed only if you plan to inject ICollection<BaseContext>
    builder.RegisterType<LATAMContext>().As<BaseContext>();
    builder.RegisterType<EUContext>().As<BaseContext>();

    builder.Populate(services);


    return new AutofacServiceProvider(builder.Build());
}

add connection strings in appsettings.json appsettings.json添加连接字符串

"ConnectionStrings": {
  "LATAMConnectionString": "Server=(localdb)\\mssqllocaldb;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true",
  "EUConnectionString": "Server=(localdb)\\mssqllocaldb;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true"
}

and now you can inject both contexts 现在您可以同时注入两种情况

public class ReportRepository : IReportRepository
{
    private readonly LATAMContext latamDbContext;
    private readonly EUContext euDbContext;

    public ReportRepository(LATAMContext latamDbContext, EUContext euDbContext)
    {
        this.latamDbContext = latamDbContext;
        this.euDbContext = euDbContext;
    }
}

or if you plan to inject collection of contexts 或者如果您打算注入上下文集合

public class ReportRepository : IReportRepository
{
    private readonly ICollection<BaseContext> dbContexts;

    public ReportRepository(ICollection<BaseContext> dbContexts)
    {
        this.dbContexts = dbContexts;
    }
}

to access specific context 访问特定上下文

var _euContext = dbContexts.FirstOrDefault(x => x is EUContext) as EUContext;
var _latamContext = dbContexts.FirstOrDefault(x => x is LATAMContext) as LATAMContext;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM