mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2024-11-23 19:42:51 +01:00
31 lines
737 B
C#
31 lines
737 B
C#
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace Phantom.Server.Database;
|
|
|
|
public sealed class DatabaseProvider {
|
|
private readonly IServiceScopeFactory serviceScopeFactory;
|
|
|
|
public DatabaseProvider(IServiceScopeFactory serviceScopeFactory) {
|
|
this.serviceScopeFactory = serviceScopeFactory;
|
|
}
|
|
|
|
public Scope CreateScope() {
|
|
return new Scope(serviceScopeFactory.CreateScope());
|
|
}
|
|
|
|
public readonly struct Scope : IDisposable {
|
|
private readonly IServiceScope scope;
|
|
|
|
public ApplicationDbContext Ctx { get; }
|
|
|
|
internal Scope(IServiceScope scope) {
|
|
this.scope = scope;
|
|
this.Ctx = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
}
|
|
|
|
public void Dispose() {
|
|
scope.Dispose();
|
|
}
|
|
}
|
|
}
|