Ir al contenido

Buscar en el Ecosistema EricksonLopez.*

Escribe el nombre de una librería, ID de paquete NuGet, tipo .NET o concepto arquitectónico para buscar en el portal.

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
Ventana de terminal
dotnet add package EricksonLopez.MultiTenancy.AspNetCore --version 2.0.0
dotnet add package EricksonLopez.MultiTenancy.PostgreSql --version 2.0.0
dotnet add package EricksonLopez.MultiTenancy.Dapper --version 2.0.0

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