Primeros Pasos con EricksonLopez.MultiTenancy
Instala EricksonLopez.MultiTenancy (2.0.0 en NuGet) junto con AspNetCore, PostgreSql y Dapper.
dotnet add package EricksonLopez.MultiTenancy --version 2.0.0<PackageReference Include="EricksonLopez.MultiTenancy" Version="2.0.0" /><PackageVersion Include="EricksonLopez.MultiTenancy" Version="2.0.0" />dotnet add package EricksonLopez.MultiTenancy.AspNetCore --version 2.0.0dotnet add package EricksonLopez.MultiTenancy.PostgreSql --version 2.0.0dotnet add package EricksonLopez.MultiTenancy.Dapper --version 2.0.0Inicio Rápido: Store de Inquilinos y RLS en PostgreSQL
Sección titulada «Inicio Rápido: Store de Inquilinos y RLS en PostgreSQL»TenantId, TenantInfo y AspNetCoreMultiTenancyExtensions viven en namespace EricksonLopez.MultiTenancy;. InMemoryTenantStore<T> registra inquilinos mediante AddOrUpdate(...):
using System.Data.Common;using System.Threading;using System.Threading.Tasks;using Dapper;using EricksonLopez.MultiTenancy;using EricksonLopez.MultiTenancy.Dapper;using EricksonLopez.MultiTenancy.PostgreSql;using Microsoft.Extensions.DependencyInjection;
namespace Example.MultiTenancyQuickStart;
public static class MultiTenancySetup{ public static void ConfigureServices(IServiceCollection services) { var acmeId = TenantId.Create("11111111-1111-1111-1111-111111111111"); services.AddAspNetCoreMultiTenancy(opt => opt.RequireTenant = true); services.AddInMemoryTenantStore<TenantInfo>(store => store.AddOrUpdate(new TenantInfo(acmeId, "acme-corp", isActive: true))); services.AddHostNameTenantStrategy(); }
public static async Task<int> QueryTenantInvoicesAsync( DbConnection connection, ITenantContext tenantContext, CancellationToken ct) { await using DbTransaction tx = await connection.BeginTenantTransactionAsync( tenantContext, cancellationToken: ct); DynamicParameters parameters = tenantContext.CreateTenantParameters(); int count = await connection.ExecuteScalarAsync<int>( "SELECT COUNT(*) FROM invoices WHERE tenant_id = @TenantId", parameters, transaction: tx); await tx.CommitAsync(ct); return count; }}