Init Git
This commit is contained in:
39
AMREZ.EOP.Infrastructures/Tenancy/TenantProvisioner.cs
Normal file
39
AMREZ.EOP.Infrastructures/Tenancy/TenantProvisioner.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using AMREZ.EOP.Abstractions.Applications.Tenancy;
|
||||
using AMREZ.EOP.Abstractions.Applications.UseCases.Tenancy;
|
||||
using AMREZ.EOP.Infrastructures.Data;
|
||||
using AMREZ.EOP.Infrastructures.Options;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace AMREZ.EOP.Infrastructures.Tenancy;
|
||||
|
||||
public sealed class TenantProvisioner : ITenantProvisioner
|
||||
{
|
||||
private readonly TenantMap _map;
|
||||
private readonly ITenantDbContextFactory _factory;
|
||||
private readonly IOptions<AuthOptions> _conn;
|
||||
|
||||
public TenantProvisioner(TenantMap map, ITenantDbContextFactory factory, IOptions<AuthOptions> conn)
|
||||
{ _map = map; _factory = factory; _conn = conn; }
|
||||
|
||||
public async Task ProvisionAsync(string tenantKey, CancellationToken ct = default)
|
||||
{
|
||||
if (!_map.TryGetBySlug(tenantKey, out var ctx))
|
||||
{
|
||||
ctx = new TenantContext
|
||||
{
|
||||
Id = tenantKey,
|
||||
Schema = tenantKey,
|
||||
ConnectionString = _conn.Value.DefaultConnection,
|
||||
Mode = AMREZ.EOP.Domain.Shared.Tenancy.TenantMode.Rls
|
||||
};
|
||||
_map.UpsertTenant(tenantKey, (TenantContext)ctx);
|
||||
}
|
||||
|
||||
if (_conn.Value.UseSchemaPerTenant)
|
||||
{
|
||||
await using var db = _factory.Create<AppDbContext>(ctx);
|
||||
await db.Database.MigrateAsync(ct); // idempotent
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user