First Commit

This commit is contained in:
Jorge Luis caudillo esparza
2026-05-09 16:36:57 -07:00
parent 96ad4c0d3e
commit d0c284d9c0
131 changed files with 4009 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.66" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.1.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ADS.Helpers.MultiTenant\ADS.Helpers.MultiTenant.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,8 @@
using System.Data;
namespace ADS.Helpers.Data.Abstractions;
public interface ITenantDbConnectionFactory
{
IDbConnection Create();
}

View File

@@ -0,0 +1,13 @@
using ADS.Helpers.Data.Abstractions;
using Microsoft.Extensions.DependencyInjection;
namespace ADS.Helpers.Data.Extensions;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddAdsTenantDb(this IServiceCollection services)
{
services.AddScoped<ITenantDbConnectionFactory, TenantDbConnectionFactory>();
return services;
}
}

View File

@@ -0,0 +1,26 @@
using System.Data;
using ADS.Helpers.Data.Abstractions;
using ADS.Helpers.MultiTenant.Context; // o .Abstractions (según donde esté tu interfaz)
using Microsoft.Data.SqlClient;
namespace ADS.Helpers.Data;
public sealed class TenantDbConnectionFactory : ITenantDbConnectionFactory
{
private readonly ITenantContextAccessor _tenantAccessor;
public TenantDbConnectionFactory(ITenantContextAccessor tenantAccessor)
=> _tenantAccessor = tenantAccessor;
public IDbConnection Create()
{
var tenant = _tenantAccessor.Current
?? throw new InvalidOperationException("TENANT_NOT_RESOLVED");
var cs = tenant.Database?.ConnectionString;
if (string.IsNullOrWhiteSpace(cs))
throw new InvalidOperationException("TENANT_CONNECTION_STRING_MISSING");
return new SqlConnection(cs);
}
}

View File

@@ -0,0 +1,15 @@
7) Usarlo en cualquier micro (GPS incluido)
En el Program.cs del micro:
using ADS.Helpers.Data.Extensions;
builder.Services.AddAdsMiddlewares(builder.Configuration); // tu pipeline tenant
builder.Services.AddAdsTenantDb();
Y en repositorios (Dapper):
using ADS.Helpers.Data.Abstractions;
using var cn = _dbFactory.Create();