First Commit
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
using System.Data;
|
||||
|
||||
namespace ADS.Helpers.MultiTenant.Data;
|
||||
|
||||
public interface IDbConnectionFactory
|
||||
{
|
||||
IDbConnection Create();
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System.Data;
|
||||
using ADS.Helpers.MultiTenant.InternalRouting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Data.SqlClient;
|
||||
|
||||
namespace ADS.Helpers.MultiTenant.Data;
|
||||
|
||||
public sealed class TenantDbConnectionFactory : IDbConnectionFactory
|
||||
{
|
||||
private readonly IHttpContextAccessor _http;
|
||||
|
||||
public TenantDbConnectionFactory(IHttpContextAccessor http) => _http = http;
|
||||
|
||||
public IDbConnection Create()
|
||||
{
|
||||
var ctx = _http.HttpContext ?? throw new InvalidOperationException("HTTP_CONTEXT_NOT_AVAILABLE");
|
||||
|
||||
if (!ctx.Items.TryGetValue(InternalTenantDbHeaders.ItemConn, out var csObj) ||
|
||||
csObj is not string cs ||
|
||||
string.IsNullOrWhiteSpace(cs))
|
||||
throw new InvalidOperationException("INTERNAL_CONN_MISSING");
|
||||
|
||||
return new SqlConnection(cs);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user