Added Scoped Handlers

This commit is contained in:
Dan Clark
2024-12-04 22:35:58 +00:00
parent 9b9330c8cc
commit 6454e1bb6f
12 changed files with 235 additions and 56 deletions
@@ -50,6 +50,52 @@ public static class IServiceCollectionExtensions
return services;
}
public static IServiceCollection AddAsyncHandlerScoped<TMessage, TResponse, THandler>(this IServiceCollection services,
string key) where THandler : class, IAsyncHandler<TMessage, TResponse>
where TMessage : class => AddAsyncHandlerScoped<TMessage, TResponse, THandler>(services, ServiceLifetime.Transient, key);
public static IServiceCollection AddAsyncHandlerScoped<TMessage, TResponse, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient,
string? key = null) where THandler : class, IAsyncHandler<TMessage, TResponse>
where TMessage : class
{
if (key is { Length: > 0 })
{
services.Add(new ServiceDescriptor(typeof(IAsyncHandler<TMessage, TResponse>), key, typeof(THandler), lifetime));
services.AddInitializationScoped<AsyncHandlerKeyedInitialization<TMessage, TResponse, IAsyncHandler<TMessage, TResponse>>>(key);
}
else
{
services.Add(new ServiceDescriptor(typeof(IAsyncHandler<TMessage, TResponse>), typeof(THandler), lifetime));
services.AddInitializationScoped<AsyncHandlerInitialization<TMessage, TResponse, IAsyncHandler<TMessage, TResponse>>>();
}
return services;
}
public static IServiceCollection AddAsyncHandlerScoped<TMessage, THandler>(this IServiceCollection services,
string key) where THandler : class, IAsyncHandler<TMessage>
where TMessage : class => AddAsyncHandlerScoped<TMessage, THandler>(services, ServiceLifetime.Transient, key);
public static IServiceCollection AddAsyncHandlerScoped<TMessage, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient,
string? key = null) where THandler : class, IAsyncHandler<TMessage>
where TMessage : class
{
if (key is { Length: > 0 })
{
services.Add(new ServiceDescriptor(typeof(IAsyncHandler<TMessage>), key, typeof(THandler), lifetime));
services.AddInitializationScoped<AsyncHandlerKeyedInitialization<TMessage, IAsyncHandler<TMessage>>>(key);
}
else
{
services.Add(new ServiceDescriptor(typeof(IAsyncHandler<TMessage>), typeof(THandler), lifetime));
services.AddInitializationScoped<AsyncHandlerInitialization<TMessage, IAsyncHandler<TMessage>>>();
}
return services;
}
public static IServiceCollection AddAsyncInitialization<TInitialization>(this IServiceCollection services)
where TInitialization : class, IAsyncInitialization
{
@@ -57,6 +103,40 @@ public static class IServiceCollectionExtensions
return services;
}
public static IServiceCollection AddAsyncPipelineBehavior(this IServiceCollection services,
Type behaviorType,
string? key = null)
{
bool ImplementsInterface(Type type, Type interfaceType) =>
type.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == interfaceType);
if (ImplementsInterface(behaviorType, typeof(IAsyncPipelineBehavior<,>)))
{
if (key is { Length: > 0 })
{
services.AddKeyedTransient(typeof(IAsyncPipelineBehavior<,>), key, behaviorType);
}
else
{
services.AddTransient(typeof(IAsyncPipelineBehavior<,>), behaviorType);
}
}
if (ImplementsInterface(behaviorType, typeof(IAsyncPipelineBehavior<>)))
{
if (key is { Length: > 0 })
{
services.AddKeyedTransient(typeof(IAsyncPipelineBehavior<>), key, behaviorType);
}
else
{
services.AddTransient(typeof(IAsyncPipelineBehavior<>), behaviorType);
}
}
return services;
}
public static IServiceCollection AddCache<TKey, TValue>(this IServiceCollection services)
where TKey : notnull
where TValue : notnull
@@ -88,7 +168,7 @@ public static class IServiceCollectionExtensions
public static IServiceCollection AddHandler<TMessage, TResponse, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient,
string? key = null) where THandler : class, IHandler<TMessage, TResponse>
string? key = null) where THandler : class, IHandler<TMessage, TResponse>
where TMessage : class
{
if (key is { Length: > 0 })
@@ -128,6 +208,52 @@ public static class IServiceCollectionExtensions
return services;
}
public static IServiceCollection AddHandlerScoped<TMessage, THandler>(this IServiceCollection services,
string key) where THandler : class, IHandler<TMessage>
where TMessage : class => AddHandlerScoped<TMessage, THandler>(services, ServiceLifetime.Transient, key);
public static IServiceCollection AddHandlerScoped<TMessage, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient,
string? key = null) where THandler : class, IHandler<TMessage>
where TMessage : class
{
if (key is { Length: > 0 })
{
services.Add(new ServiceDescriptor(typeof(IHandler<TMessage>), key, typeof(THandler), lifetime));
services.AddInitializationScoped<HandlerKeyedInitialization<TMessage, IHandler<TMessage>>>(key);
}
else
{
services.Add(new ServiceDescriptor(typeof(IHandler<TMessage>), typeof(THandler), lifetime));
services.AddInitializationScoped<HandlerInitialization<TMessage, IHandler<TMessage>>>();
}
return services;
}
public static IServiceCollection AddHandlerScoped<TMessage, TResponse, THandler>(this IServiceCollection services,
string key) where THandler : class, IHandler<TMessage, TResponse>
where TMessage : class => AddHandlerScoped<TMessage, TResponse, THandler>(services, ServiceLifetime.Transient, key);
public static IServiceCollection AddHandlerScoped<TMessage, TResponse, THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient,
string? key = null) where THandler : class, IHandler<TMessage, TResponse>
where TMessage : class
{
if (key is { Length: > 0 })
{
services.Add(new ServiceDescriptor(typeof(IHandler<TMessage, TResponse>), key, typeof(THandler), lifetime));
services.AddInitializationScoped<HandlerKeyedInitialization<TMessage, TResponse, IHandler<TMessage, TResponse>>>(key);
}
else
{
services.Add(new ServiceDescriptor(typeof(IHandler<TMessage, TResponse>), typeof(THandler), lifetime));
services.AddInitializationScoped<HandlerInitialization<TMessage, TResponse, IHandler<TMessage, TResponse>>>();
}
return services;
}
public static IServiceCollection AddInitialization<TInitialization, TInitializationImplementation>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient)
where TInitialization : class, IInitialization
@@ -155,45 +281,44 @@ public static class IServiceCollectionExtensions
return services;
}
public static IServiceCollection AddInitialization(this IServiceCollection services,
public static IServiceCollection AddInitialization(this IServiceCollection services,
Action<IServiceProvider> delegateAction)
{
services.AddTransient<IInitialization>(provider => new ActionableInitialization(provider, delegateAction));
return services;
}
public static IServiceCollection AddAsyncPipelineBehavior(this IServiceCollection services,
Type behaviorType,
string? key = null)
public static IServiceCollection AddInitializationScoped<TInitialization, TInitializationImplementation>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient)
where TInitialization : class, IInitialization
where TInitializationImplementation : class, IInitializationScoped
{
bool ImplementsInterface(Type type, Type interfaceType) =>
type.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == interfaceType);
services.Add(new ServiceDescriptor(typeof(IInitializationScoped), typeof(TInitializationImplementation), lifetime));
services.AddTransient<IInitializationScoped>(provider => provider.GetRequiredService<IInitializationScoped>());
return services;
}
if (ImplementsInterface(behaviorType, typeof(IAsyncPipelineBehavior<,>)))
{
if (key is { Length: > 0 })
{
services.AddKeyedTransient(typeof(IAsyncPipelineBehavior<,>), key, behaviorType);
}
else
{
services.AddTransient(typeof(IAsyncPipelineBehavior<,>), behaviorType);
}
}
if (ImplementsInterface(behaviorType, typeof(IAsyncPipelineBehavior<>)))
{
if (key is { Length: > 0 })
{
services.AddKeyedTransient(typeof(IAsyncPipelineBehavior<>), key, behaviorType);
}
else
{
services.AddTransient(typeof(IAsyncPipelineBehavior<>), behaviorType);
}
}
public static IServiceCollection AddInitializationScoped<TInitialization>(this IServiceCollection services)
where TInitialization : class, IInitializationScoped
{
services.AddTransient<IInitializationScoped, TInitialization>();
return services;
}
public static IServiceCollection AddInitializationScoped<TInitialization>(this IServiceCollection services,
params object[] parameters)
where TInitialization : class, IInitializationScoped
{
services.AddTransient<IInitializationScoped>(provider => provider.GetRequiredService<IServiceFactory>()
.Create<TInitialization>(parameters));
return services;
}
public static IServiceCollection AddInitializationScoped(this IServiceCollection services,
Action<IServiceProvider> delegateAction)
{
services.AddTransient<IInitializationScoped>(provider => new ActionableInitializationScoped(provider, delegateAction));
return services;
}
@@ -208,6 +333,34 @@ public static class IServiceCollectionExtensions
return services;
}
public static IServiceCollection AddServiceScope<TServiceScope>(this IServiceCollection services)
where TServiceScope : notnull
{
services.AddCache<TServiceScope, IServiceScope>();
services.AddTransient<IServiceScopeProvider<TServiceScope>, ServiceScopeProvider<TServiceScope>>();
services.AddTransient<IServiceScopeFactory<TServiceScope>, ServiceScopeFactory<TServiceScope>>();
return services;
}
public static IServiceCollection AddServiceScope<TServiceScope>(this IServiceCollection services,
Action<IServiceProvider> providerDelegate)
where TServiceScope : notnull
{
services.AddCache<TServiceScope, IServiceScope>();
services.AddTransient<IServiceScopeProvider<TServiceScope>, ServiceScopeProvider<TServiceScope>>();
services.AddTransient<IServiceScopeFactory<TServiceScope>, ServiceScopeFactory<TServiceScope>>(provider =>
{
providerDelegate.Invoke(provider);
IServiceScopeFactory factory = provider.GetRequiredService<IServiceScopeFactory>();
ICache<TServiceScope, IServiceScope> cache = provider.GetRequiredService<ICache<TServiceScope, IServiceScope>>();
return new ServiceScopeFactory<TServiceScope>(factory, cache);
});
return services;
}
public static IServiceCollection AddTemplate<TViewModel, TView>(this IServiceCollection services,
object? key = null,
ServiceLifetime serviceLifetime = ServiceLifetime.Transient,