Added Scoped Handlers
This commit is contained in:
@@ -2,20 +2,25 @@
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class ServiceScopeFactory<TService>(IServiceScopeFactory serviceScopeFactory,
|
||||
ICache<TService, IServiceScope> cache) :
|
||||
IServiceScopeFactory<TService>
|
||||
where TService : notnull
|
||||
public class ServiceScopeFactory<TServiceScope>(IServiceScopeFactory serviceScopeFactory,
|
||||
ICache<TServiceScope, IServiceScope> cache) :
|
||||
IServiceScopeFactory<TServiceScope>
|
||||
where TServiceScope : notnull
|
||||
{
|
||||
public (IServiceScope, TService) Create(params object?[] parameters)
|
||||
public (IServiceScope, TServiceScope) Create(params object?[] parameters)
|
||||
{
|
||||
if (serviceScopeFactory.CreateScope() is IServiceScope serviceScope)
|
||||
{
|
||||
if (serviceScope.ServiceProvider.GetService<IServiceFactory>() is IServiceFactory factory)
|
||||
{
|
||||
if (factory.Create<TService>(parameters) is TService service)
|
||||
if (factory.Create<TServiceScope>(parameters) is TServiceScope service)
|
||||
{
|
||||
cache.Add(service, serviceScope);
|
||||
foreach (IInitializationScoped initializationScoped in serviceScope.ServiceProvider.GetServices<IInitializationScoped>())
|
||||
{
|
||||
initializationScoped.Initialize();
|
||||
}
|
||||
|
||||
return (serviceScope, service);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user