mirror of
https://github.com/C9Glax/tranga.git
synced 2025-10-17 10:50:45 +02:00
This commit is contained in:
28
API/Workers/BaseWorkerWithContexts.cs
Normal file
28
API/Workers/BaseWorkerWithContexts.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user