Шаблон репозитория с несколькими базами данных - PullRequest
1 голос
/ 03 мая 2019

Я использую шаблон репозитория на EF Core и Autofac в службе Windows.

У меня есть служба, которая должна соединиться с несколькими десятками баз данных, которые имеют одинаковую схему (один и тот же dbcontext), но только разные данные. Как я могу добиться этого в моем сервисе с помощью Autofac? Бела

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
        }       
    }

1 Ответ

2 голосов
/ 03 мая 2019

Создание базового контекста, включая все настройки, наборы данных и т. Д .:

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

наследуется от BaseContext для обеих БД

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

    }
}

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

    }
}

и зарегистрируйтесь как в 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());
}

добавить строки подключения в 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"
}

и теперь вы можете ввести оба контекста

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

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

или если вы планируете внедрить коллекцию контекстов

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

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

для доступа к конкретному контексту

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