Added Scoped Handlers
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class ActionableInitializationScoped(IServiceProvider provider,
|
||||
Action<IServiceProvider> delegateAction) : IInitializationScoped
|
||||
{
|
||||
public void Initialize() => delegateAction.Invoke(provider);
|
||||
}
|
||||
@@ -5,7 +5,7 @@ namespace Toolkit.Foundation;
|
||||
|
||||
public class AsyncHandlerInitialization<TMessage, TResponse, THandler>(IMessenger messenger,
|
||||
IServiceProvider provider) :
|
||||
IInitialization where THandler : class, IAsyncHandler<TMessage, TResponse>
|
||||
IInitialization, IInitializationScoped where THandler : class, IAsyncHandler<TMessage, TResponse>
|
||||
where TMessage : class
|
||||
{
|
||||
public void Initialize()
|
||||
@@ -33,7 +33,7 @@ public class AsyncHandlerInitialization<TMessage, TResponse, THandler>(IMessenge
|
||||
|
||||
public class AsyncHandlerInitialization<TMessage, THandler>(IMessenger messenger,
|
||||
IServiceProvider provider) :
|
||||
IInitialization where THandler : class, IAsyncHandler<TMessage>
|
||||
IInitialization, IInitializationScoped where THandler : class, IAsyncHandler<TMessage>
|
||||
where TMessage : class
|
||||
{
|
||||
public void Initialize()
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace Toolkit.Foundation;
|
||||
public class AsyncHandlerKeyedInitialization<TMessage, THandler>(string key,
|
||||
IMessenger messenger,
|
||||
IServiceProvider provider) :
|
||||
IInitialization where THandler : class, IAsyncHandler<TMessage>
|
||||
IInitialization, IInitializationScoped where THandler : class, IAsyncHandler<TMessage>
|
||||
where TMessage : class
|
||||
{
|
||||
public void Initialize()
|
||||
@@ -34,7 +34,7 @@ public class AsyncHandlerKeyedInitialization<TMessage, THandler>(string key,
|
||||
|
||||
public class AsyncHandlerKeyedInitialization<TMessage, TResponse, THandler>(string key, IMessenger messenger,
|
||||
IServiceProvider provider) :
|
||||
IInitialization where THandler : class, IAsyncHandler<TMessage, TResponse>
|
||||
IInitialization, IInitializationScoped where THandler : class, IAsyncHandler<TMessage, TResponse>
|
||||
where TMessage : class
|
||||
{
|
||||
public void Initialize()
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Toolkit.Foundation;
|
||||
|
||||
public class HandlerInitialization<TMessage, TResponse, THandler>(IMessenger messenger,
|
||||
IServiceProvider provider) :
|
||||
IInitialization where THandler : class, IHandler<TMessage, TResponse>
|
||||
IInitialization, IInitializationScoped where THandler : class, IHandler<TMessage, TResponse>
|
||||
where TMessage : class
|
||||
{
|
||||
public void Initialize()
|
||||
@@ -34,7 +34,7 @@ public class HandlerInitialization<TMessage, TResponse, THandler>(IMessenger mes
|
||||
|
||||
public class HandlerInitialization<TMessage, THandler>(IMessenger messenger,
|
||||
IServiceProvider provider) :
|
||||
IInitialization where THandler : class, IHandler<TMessage>
|
||||
IInitialization, IInitializationScoped where THandler : class, IHandler<TMessage>
|
||||
where TMessage : class
|
||||
{
|
||||
public void Initialize()
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace Toolkit.Foundation;
|
||||
public class HandlerKeyedInitialization<TMessage, THandler>(string key,
|
||||
IMessenger messenger,
|
||||
IServiceProvider provider) :
|
||||
IInitialization where THandler : class, IHandler<TMessage>
|
||||
IInitialization, IInitializationScoped where THandler : class, IHandler<TMessage>
|
||||
where TMessage : class
|
||||
{
|
||||
public void Initialize()
|
||||
@@ -46,7 +46,7 @@ public class HandlerKeyedInitialization<TMessage, THandler>(string key,
|
||||
public class HandlerKeyedInitialization<TMessage, TResponse, THandler>(string key,
|
||||
IMessenger messenger,
|
||||
IServiceProvider provider) :
|
||||
IInitialization where THandler : class, IHandler<TMessage, TResponse>
|
||||
IInitialization, IInitializationScoped where THandler : class, IHandler<TMessage, TResponse>
|
||||
where TMessage : class
|
||||
{
|
||||
public void Initialize()
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IInitializationScoped
|
||||
{
|
||||
void Initialize();
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IDecoratorService<T>
|
||||
public interface IScopedServiceDescriptor<T>
|
||||
{
|
||||
T? Value { get; }
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -157,43 +283,42 @@ public static class IServiceCollectionExtensions
|
||||
|
||||
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);
|
||||
|
||||
if (ImplementsInterface(behaviorType, typeof(IAsyncPipelineBehavior<,>)))
|
||||
{
|
||||
if (key is { Length: > 0 })
|
||||
{
|
||||
services.AddKeyedTransient(typeof(IAsyncPipelineBehavior<,>), key, behaviorType);
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddTransient(typeof(IAsyncPipelineBehavior<,>), behaviorType);
|
||||
}
|
||||
services.Add(new ServiceDescriptor(typeof(IInitializationScoped), typeof(TInitializationImplementation), lifetime));
|
||||
services.AddTransient<IInitializationScoped>(provider => provider.GetRequiredService<IInitializationScoped>());
|
||||
return services;
|
||||
}
|
||||
|
||||
if (ImplementsInterface(behaviorType, typeof(IAsyncPipelineBehavior<>)))
|
||||
public static IServiceCollection AddInitializationScoped<TInitialization>(this IServiceCollection services)
|
||||
where TInitialization : class, IInitializationScoped
|
||||
{
|
||||
if (key is { Length: > 0 })
|
||||
{
|
||||
services.AddKeyedTransient(typeof(IAsyncPipelineBehavior<>), key, behaviorType);
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddTransient(typeof(IAsyncPipelineBehavior<>), behaviorType);
|
||||
}
|
||||
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,
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class DecoratorService<T> :
|
||||
IDecoratorService<T>
|
||||
public class ScopedServiceDescriptor<T> :
|
||||
IScopedServiceDescriptor<T>
|
||||
{
|
||||
public T? Value { get; private set; }
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ public class ContentTemplate :
|
||||
DataTemplateSelector,
|
||||
IContentTemplate
|
||||
{
|
||||
private readonly Dictionary<string, DataTemplate> _cache = [];
|
||||
|
||||
protected override DataTemplate? SelectTemplateCore(object item)
|
||||
{
|
||||
if (item is IObservableViewModel observableViewModel)
|
||||
@@ -17,10 +19,19 @@ public class ContentTemplate :
|
||||
if (observableViewModel.Provider is IServiceProvider provider)
|
||||
{
|
||||
Type itemType = item.GetType();
|
||||
if (provider.GetRequiredKeyedService<IContentTemplateDescriptor>(itemType.Name.Replace("ViewModel", ""))
|
||||
string key = itemType.Name.Replace("ViewModel", "");
|
||||
|
||||
if (_cache.TryGetValue(key, out DataTemplate? cachedTemplate))
|
||||
{
|
||||
return cachedTemplate;
|
||||
}
|
||||
|
||||
if (provider.GetRequiredKeyedService<IContentTemplateDescriptor>(key)
|
||||
is IContentTemplateDescriptor descriptor)
|
||||
{
|
||||
return CreateDataTemplate(descriptor);
|
||||
var newTemplate = CreateDataTemplate(descriptor);
|
||||
_cache[key] = newTemplate;
|
||||
return newTemplate;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,10 +40,7 @@ public class ContentTemplate :
|
||||
}
|
||||
|
||||
protected override DataTemplate? SelectTemplateCore(object item,
|
||||
DependencyObject container)
|
||||
{
|
||||
return SelectTemplateCore(item);
|
||||
}
|
||||
DependencyObject container) => SelectTemplateCore(item);
|
||||
|
||||
private static DataTemplate CreateDataTemplate(IContentTemplateDescriptor descriptor)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user