Skip to content

Commit f91acea

Browse files
committed
Add database scope support to http host extension
1 parent 8cb9c5b commit f91acea

4 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/Dibix.Http.Host/Runtime/DatabaseScope.cs renamed to src/Dibix.Http.Host/Data/DatabaseScope.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ public string InitiatorFullName
1818
set => _initiatorFullName = value;
1919
}
2020
}
21-
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Dibix.Http.Server;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace Dibix.Http.Host
5+
{
6+
internal sealed class DatabaseScopeFactory : IDatabaseScopeFactory
7+
{
8+
private readonly IServiceScopeFactory _scopeFactory;
9+
10+
public DatabaseScopeFactory(IServiceScopeFactory scopeFactory)
11+
{
12+
_scopeFactory = scopeFactory;
13+
}
14+
15+
public IDatabaseAccessorFactory Create<TInitiator>() => Create(typeof(TInitiator).FullName!);
16+
public IDatabaseAccessorFactory Create(string initiatorFullName)
17+
{
18+
IServiceScope serviceScope = _scopeFactory.CreateScope();
19+
DatabaseScope databaseScope = serviceScope.ServiceProvider.GetRequiredService<DatabaseScope>();
20+
databaseScope.InitiatorFullName = initiatorFullName;
21+
IDatabaseAccessorFactory databaseAccessorFactory = serviceScope.ServiceProvider.GetRequiredService<IDatabaseAccessorFactory>();
22+
return databaseAccessorFactory;
23+
}
24+
}
25+
}

src/Dibix.Http.Host/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ private static async Task Main(string[] args)
3131
.AddScoped<IDatabaseAccessorFactory, ScopedDatabaseAccessorFactory>()
3232
.AddScoped<DatabaseScope>()
3333
.AddScoped<CreateDatabaseLogger>(x => () => x.GetRequiredService<ILoggerFactory>().CreateLogger(x.GetRequiredService<DatabaseScope>().InitiatorFullName))
34+
.AddSingleton<IDatabaseScopeFactory, DatabaseScopeFactory>()
3435
.AddSingleton<HttpApiRegistryFactory>()
3536
.AddSingleton<IHttpApiRegistry>(z => z.GetRequiredService<HttpApiRegistryFactory>().Create())
3637
.AddSingleton<IEndpointUrlBuilder, AssemblyEndpointConfigurationUrlBuilder>()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Dibix.Http.Server
2+
{
3+
public interface IDatabaseScopeFactory
4+
{
5+
IDatabaseAccessorFactory Create<TInitiator>();
6+
IDatabaseAccessorFactory Create(string initiatorFullName);
7+
}
8+
}

0 commit comments

Comments
 (0)