tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
DatabaseContextFactory.cs
Go to the documentation of this file.
1using System;
2using System.Threading.Tasks;
3
4using Microsoft.Extensions.DependencyInjection;
5
7{
10 {
14 readonly IServiceScopeFactory scopeFactory;
15
20 public DatabaseContextFactory(IServiceScopeFactory scopeFactory)
21 {
22 this.scopeFactory = scopeFactory ?? throw new ArgumentNullException(nameof(scopeFactory));
23
24 using var scope = scopeFactory.CreateScope();
25 scope.ServiceProvider.GetRequiredService<IDatabaseContext>();
26 }
27
29 public async ValueTask UseContext(Func<IDatabaseContext, ValueTask> operation)
30 {
31 ArgumentNullException.ThrowIfNull(operation);
32
33 await using var scope = scopeFactory.CreateAsyncScope();
34 await operation(scope.ServiceProvider.GetRequiredService<IDatabaseContext>());
35 }
36
38 public async ValueTask UseContextTaskReturn(Func<IDatabaseContext, Task> operation)
39 {
40 ArgumentNullException.ThrowIfNull(operation);
41
42 await using var scope = scopeFactory.CreateAsyncScope();
43 await operation(scope.ServiceProvider.GetRequiredService<IDatabaseContext>());
44 }
45 }
46}
readonly IServiceScopeFactory scopeFactory
The IServiceScopeFactory for the DatabaseContextFactory.
async ValueTask UseContext(Func< IDatabaseContext, ValueTask > operation)
Run an operation in the scope of an IDatabaseContext.A ValueTask representing the running operation ...
async ValueTask UseContextTaskReturn(Func< IDatabaseContext, Task > operation)
Run an operation in the scope of an IDatabaseContext.A ValueTask representing the running operation ...
DatabaseContextFactory(IServiceScopeFactory scopeFactory)
Initializes a new instance of the DatabaseContextFactory class.
Factory for scoping usage of IDatabaseContexts. Meant for use by Components.