Files
Tranga/API/Workers/BaseWorkerWithContexts.cs
glax 53276e858b
Some checks are pending
Docker Image CI / build (push) Waiting to run
Actions initial commit
2025-10-16 02:53:02 +02:00

28 lines
1000 B
C#

using Microsoft.EntityFrameworkCore;
namespace API.Workers;
public abstract class BaseWorkerWithContexts(IEnumerable<BaseWorker>? dependsOn = null) : BaseWorker(dependsOn)
{
/// <summary>
/// Returns the context of requested type <typeparamref name="T"/>
/// </summary>
/// <param name="scope"></param>
/// <typeparam name="T">Type of <see cref="DbContext"/></typeparam>
/// <returns>Context in scope</returns>
/// <exception cref="Exception">Scope not set</exception>
protected T GetContext<T>(IServiceScope scope) where T : DbContext
{
if (scope is not { } serviceScope)
throw new Exception("Scope not set!");
return serviceScope.ServiceProvider.GetRequiredService<T>();
}
protected abstract void SetContexts(IServiceScope serviceScope);
public new Task<BaseWorker[]> DoWork(IServiceScope serviceScope, Action? callback = null)
{
SetContexts(serviceScope);
return base.DoWork(callback);
}
}