tgstation-server 5.12.7
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 Task UseContext(Func<IDatabaseContext, Task> operation)
30 {
31 ArgumentNullException.ThrowIfNull(operation);
32
33 await using var scope = scopeFactory.CreateAsyncScope();
34 await operation(scope.ServiceProvider.GetRequiredService<IDatabaseContext>());
35 }
36 }
37}
async Task UseContext(Func< IDatabaseContext, Task > operation)
Run an operation in the scope of an IDatabaseContext. A Task representing the running operation .
readonly IServiceScopeFactory scopeFactory
The IServiceScopeFactory for the DatabaseContextFactory.
DatabaseContextFactory(IServiceScopeFactory scopeFactory)
Initializes a new instance of the DatabaseContextFactory class.
Factory for scoping usage of IDatabaseContexts. Meant for use by Components.