我有两个共享相同存储库的DbContext。以下是我现有的代码:
Startup.cs
services.AddDbContext<SgAesMasterContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SgAesMasterContext"),
b => b.UseRowNumberForPaging()), ServiceLifetime.Scoped);
services.AddDbContext<MyAesMasterContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyAesMasterContext"),
b => b.UseRowNumberForPaging()), ServiceLifetime.Scoped);
DbContext.cs
public class SgAesMasterContext : DbContext
{
public DbSet<Company> Companies { get; set; }
public SgAesMasterContext(DbContextOptions<SgAesMasterContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
}
public class MyAesMasterContext : DbContext
{
public DbSet<Company> Companies { get; set; }
public MyAesMasterContext(DbContextOptions<MyAesMasterContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
}
UnitOfWork.cs
public class AesMasterUnitOfWork: IAesMasterUnitOfWork
{
private readonly SgAesMasterContext sgAesMasterContext;
private readonly MyAesMasterContext myAesMasterContext;
public AesMasterUnitOfWork(SgAesMasterContext sgAesMasterContext, MyAesMasterContext myAesMasterContext)
{
this.sgAesMasterContext = sgAesMasterContext;
this.myAesMasterContext = myAesMasterContext;
}
public SgAesMasterContext SgAesMasterContext { get { return sgAesMasterContext; } }
public MyAesMasterContext MyAesMasterContext { get { return myAesMasterContext; } }
public CompanyRepository CompanyRepo { get { return new CompanyRepository(sgAesMasterContext, myAesMasterContext); } }
public void Dispose()
{
sgAesMasterContext.Dispose();
myAesMasterContext.Dispose();
}
}
CompanyRepository.cs
public class CompanyRepository
{
protected readonly SgAesMasterContext sgAesMasterContext;
protected readonly MyAesMasterContext myAesMasterContext;
public CompanyRepository(SgAesMasterContext sgAesMasterContext, MyAesMasterContext myAesMasterContext)
{
this.sgAesMasterContext = sgAesMasterContext;
this.myAesMasterContext = myAesMasterContext;
}
public async Task<List<Company>> GetCompanies(string country)
{
List<Company> companies = new List<Company>();
switch (country)
{
case "SG":
companies = await sgAesMasterContext.Companies.Where(x => x.Company_Status == "A" && x.Status == "0").ToListAsync();
break;
case "MY":
companies = await myAesMasterContext.Companies.Where(x => x.Company_Status == "A" && x.Status == "0").ToListAsync();
break;
}
return companies;
}
}
有什么解决方案可以使我的存储库调用动态DbContext而不使用切换用例吗?这是因为,如果我对存储库中的每个功能都使用开关盒,它将变得多余并且难以维护。
答案 0 :(得分:2)
此解决方案适用于多租户类型的方案,在这种方案中,您具有相同的架构,需要在基于该租户的不同数据库上运行相同的查询。
您可以只有一个DbContext,但可以动态传递ConnectionString以连接到不同的数据库。
由于只有一个DbContext,因此所有存储库都将依赖于该DbContext。尽管您需要根据租户将正确的连接字符串传递给DbContext。
您可以创建并实现ITenantDbContextFactory
,该请求将调查请求以识别要使用的ConnectionString并基于该字符串创建DbContext
去离子注入中的设置ITenantDbContextFactory
public class TenantDbContextFactory : ITenantDbContextFactory
{
public TenantDbContext GetDbContext()
{
string country;
// Read Request to identify correct tenant
switch (country)
{
case "SG":
return new TenantDbContext("connectionstring for Sg")
break;
case "MY":
return new TenantDbContext("connectionstring for my")
break;
}
}
}
public class TenantDbContext: DbContext
{
public DbSet<Company> Companies { get; set; }
public TenantDbContext(DbContextOptions<TenantDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
}
public class TenantUnitOfWork: ITenantUnitOfWork
{
private readonly TenantDbContext tenantDbContext;
public TenantUnitOfWork(ITenantDbContextFactory tenantDbContextFactory)
{
this.tenantDbContext= tenantDbContextFactory.GetDbContext();
}
public TenantDbContext TenantDbContext { get { return tenantDbContext; } }
public CompanyRepository CompanyRepo { get { return new CompanyRepository(tenantDbContext); } }
public void Dispose()
{
tenantDbContext.Dispose();
}
}
public class CompanyRepository
{
protected readonly TenantDbContext tenantDbContext ;
public CompanyRepository(TenantDbContext tenantDbContext)
{
this.tenantDbContext = tenantDbContext ;
}
public async Task<List<Company>> GetCompanies()
{
List<Company> companies = new List<Company>();
companies = await tenantDbContext.Companies.Where(x => x.Company_Status == "A" && x.Status == "0").ToListAsync();
return companies;
}
}