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,
|
public class AsyncHandlerInitialization<TMessage, TResponse, THandler>(IMessenger messenger,
|
||||||
IServiceProvider provider) :
|
IServiceProvider provider) :
|
||||||
IInitialization where THandler : class, IAsyncHandler<TMessage, TResponse>
|
IInitialization, IInitializationScoped where THandler : class, IAsyncHandler<TMessage, TResponse>
|
||||||
where TMessage : class
|
where TMessage : class
|
||||||
{
|
{
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
@@ -33,7 +33,7 @@ public class AsyncHandlerInitialization<TMessage, TResponse, THandler>(IMessenge
|
|||||||
|
|
||||||
public class AsyncHandlerInitialization<TMessage, THandler>(IMessenger messenger,
|
public class AsyncHandlerInitialization<TMessage, THandler>(IMessenger messenger,
|
||||||
IServiceProvider provider) :
|
IServiceProvider provider) :
|
||||||
IInitialization where THandler : class, IAsyncHandler<TMessage>
|
IInitialization, IInitializationScoped where THandler : class, IAsyncHandler<TMessage>
|
||||||
where TMessage : class
|
where TMessage : class
|
||||||
{
|
{
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace Toolkit.Foundation;
|
|||||||
public class AsyncHandlerKeyedInitialization<TMessage, THandler>(string key,
|
public class AsyncHandlerKeyedInitialization<TMessage, THandler>(string key,
|
||||||
IMessenger messenger,
|
IMessenger messenger,
|
||||||
IServiceProvider provider) :
|
IServiceProvider provider) :
|
||||||
IInitialization where THandler : class, IAsyncHandler<TMessage>
|
IInitialization, IInitializationScoped where THandler : class, IAsyncHandler<TMessage>
|
||||||
where TMessage : class
|
where TMessage : class
|
||||||
{
|
{
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
@@ -34,7 +34,7 @@ public class AsyncHandlerKeyedInitialization<TMessage, THandler>(string key,
|
|||||||
|
|
||||||
public class AsyncHandlerKeyedInitialization<TMessage, TResponse, THandler>(string key, IMessenger messenger,
|
public class AsyncHandlerKeyedInitialization<TMessage, TResponse, THandler>(string key, IMessenger messenger,
|
||||||
IServiceProvider provider) :
|
IServiceProvider provider) :
|
||||||
IInitialization where THandler : class, IAsyncHandler<TMessage, TResponse>
|
IInitialization, IInitializationScoped where THandler : class, IAsyncHandler<TMessage, TResponse>
|
||||||
where TMessage : class
|
where TMessage : class
|
||||||
{
|
{
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ namespace Toolkit.Foundation;
|
|||||||
|
|
||||||
public class HandlerInitialization<TMessage, TResponse, THandler>(IMessenger messenger,
|
public class HandlerInitialization<TMessage, TResponse, THandler>(IMessenger messenger,
|
||||||
IServiceProvider provider) :
|
IServiceProvider provider) :
|
||||||
IInitialization where THandler : class, IHandler<TMessage, TResponse>
|
IInitialization, IInitializationScoped where THandler : class, IHandler<TMessage, TResponse>
|
||||||
where TMessage : class
|
where TMessage : class
|
||||||
{
|
{
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
@@ -34,7 +34,7 @@ public class HandlerInitialization<TMessage, TResponse, THandler>(IMessenger mes
|
|||||||
|
|
||||||
public class HandlerInitialization<TMessage, THandler>(IMessenger messenger,
|
public class HandlerInitialization<TMessage, THandler>(IMessenger messenger,
|
||||||
IServiceProvider provider) :
|
IServiceProvider provider) :
|
||||||
IInitialization where THandler : class, IHandler<TMessage>
|
IInitialization, IInitializationScoped where THandler : class, IHandler<TMessage>
|
||||||
where TMessage : class
|
where TMessage : class
|
||||||
{
|
{
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace Toolkit.Foundation;
|
|||||||
public class HandlerKeyedInitialization<TMessage, THandler>(string key,
|
public class HandlerKeyedInitialization<TMessage, THandler>(string key,
|
||||||
IMessenger messenger,
|
IMessenger messenger,
|
||||||
IServiceProvider provider) :
|
IServiceProvider provider) :
|
||||||
IInitialization where THandler : class, IHandler<TMessage>
|
IInitialization, IInitializationScoped where THandler : class, IHandler<TMessage>
|
||||||
where TMessage : class
|
where TMessage : class
|
||||||
{
|
{
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
@@ -46,7 +46,7 @@ public class HandlerKeyedInitialization<TMessage, THandler>(string key,
|
|||||||
public class HandlerKeyedInitialization<TMessage, TResponse, THandler>(string key,
|
public class HandlerKeyedInitialization<TMessage, TResponse, THandler>(string key,
|
||||||
IMessenger messenger,
|
IMessenger messenger,
|
||||||
IServiceProvider provider) :
|
IServiceProvider provider) :
|
||||||
IInitialization where THandler : class, IHandler<TMessage, TResponse>
|
IInitialization, IInitializationScoped where THandler : class, IHandler<TMessage, TResponse>
|
||||||
where TMessage : class
|
where TMessage : class
|
||||||
{
|
{
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
|
|||||||
@@ -3,4 +3,4 @@
|
|||||||
public interface IInitialization
|
public interface IInitialization
|
||||||
{
|
{
|
||||||
void Initialize();
|
void Initialize();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
|
public interface IInitializationScoped
|
||||||
|
{
|
||||||
|
void Initialize();
|
||||||
|
}
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
namespace Toolkit.Foundation;
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
public interface IDecoratorService<T>
|
public interface IScopedServiceDescriptor<T>
|
||||||
{
|
{
|
||||||
T? Value { get; }
|
T? Value { get; }
|
||||||
|
|
||||||
@@ -50,6 +50,52 @@ public static class IServiceCollectionExtensions
|
|||||||
return services;
|
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)
|
public static IServiceCollection AddAsyncInitialization<TInitialization>(this IServiceCollection services)
|
||||||
where TInitialization : class, IAsyncInitialization
|
where TInitialization : class, IAsyncInitialization
|
||||||
{
|
{
|
||||||
@@ -57,6 +103,40 @@ public static class IServiceCollectionExtensions
|
|||||||
return services;
|
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)
|
public static IServiceCollection AddCache<TKey, TValue>(this IServiceCollection services)
|
||||||
where TKey : notnull
|
where TKey : notnull
|
||||||
where TValue : notnull
|
where TValue : notnull
|
||||||
@@ -88,7 +168,7 @@ public static class IServiceCollectionExtensions
|
|||||||
|
|
||||||
public static IServiceCollection AddHandler<TMessage, TResponse, THandler>(this IServiceCollection services,
|
public static IServiceCollection AddHandler<TMessage, TResponse, THandler>(this IServiceCollection services,
|
||||||
ServiceLifetime lifetime = ServiceLifetime.Transient,
|
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
|
where TMessage : class
|
||||||
{
|
{
|
||||||
if (key is { Length: > 0 })
|
if (key is { Length: > 0 })
|
||||||
@@ -128,6 +208,52 @@ public static class IServiceCollectionExtensions
|
|||||||
return services;
|
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,
|
public static IServiceCollection AddInitialization<TInitialization, TInitializationImplementation>(this IServiceCollection services,
|
||||||
ServiceLifetime lifetime = ServiceLifetime.Transient)
|
ServiceLifetime lifetime = ServiceLifetime.Transient)
|
||||||
where TInitialization : class, IInitialization
|
where TInitialization : class, IInitialization
|
||||||
@@ -155,45 +281,44 @@ public static class IServiceCollectionExtensions
|
|||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IServiceCollection AddInitialization(this IServiceCollection services,
|
public static IServiceCollection AddInitialization(this IServiceCollection services,
|
||||||
Action<IServiceProvider> delegateAction)
|
Action<IServiceProvider> delegateAction)
|
||||||
|
|
||||||
{
|
{
|
||||||
services.AddTransient<IInitialization>(provider => new ActionableInitialization(provider, delegateAction));
|
services.AddTransient<IInitialization>(provider => new ActionableInitialization(provider, delegateAction));
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IServiceCollection AddAsyncPipelineBehavior(this IServiceCollection services,
|
public static IServiceCollection AddInitializationScoped<TInitialization, TInitializationImplementation>(this IServiceCollection services,
|
||||||
Type behaviorType,
|
ServiceLifetime lifetime = ServiceLifetime.Transient)
|
||||||
string? key = null)
|
where TInitialization : class, IInitialization
|
||||||
|
where TInitializationImplementation : class, IInitializationScoped
|
||||||
{
|
{
|
||||||
bool ImplementsInterface(Type type, Type interfaceType) =>
|
services.Add(new ServiceDescriptor(typeof(IInitializationScoped), typeof(TInitializationImplementation), lifetime));
|
||||||
type.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == interfaceType);
|
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.AddTransient<IInitializationScoped, TInitialization>();
|
||||||
services.AddKeyedTransient(typeof(IAsyncPipelineBehavior<,>), key, behaviorType);
|
return services;
|
||||||
}
|
}
|
||||||
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,
|
||||||
|
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;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,6 +333,34 @@ public static class IServiceCollectionExtensions
|
|||||||
return services;
|
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,
|
public static IServiceCollection AddTemplate<TViewModel, TView>(this IServiceCollection services,
|
||||||
object? key = null,
|
object? key = null,
|
||||||
ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
|
ServiceLifetime serviceLifetime = ServiceLifetime.Transient,
|
||||||
|
|||||||
+2
-2
@@ -1,7 +1,7 @@
|
|||||||
namespace Toolkit.Foundation;
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
public class DecoratorService<T> :
|
public class ScopedServiceDescriptor<T> :
|
||||||
IDecoratorService<T>
|
IScopedServiceDescriptor<T>
|
||||||
{
|
{
|
||||||
public T? Value { get; private set; }
|
public T? Value { get; private set; }
|
||||||
|
|
||||||
@@ -2,20 +2,25 @@
|
|||||||
|
|
||||||
namespace Toolkit.Foundation;
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
public class ServiceScopeFactory<TService>(IServiceScopeFactory serviceScopeFactory,
|
public class ServiceScopeFactory<TServiceScope>(IServiceScopeFactory serviceScopeFactory,
|
||||||
ICache<TService, IServiceScope> cache) :
|
ICache<TServiceScope, IServiceScope> cache) :
|
||||||
IServiceScopeFactory<TService>
|
IServiceScopeFactory<TServiceScope>
|
||||||
where TService : notnull
|
where TServiceScope : notnull
|
||||||
{
|
{
|
||||||
public (IServiceScope, TService) Create(params object?[] parameters)
|
public (IServiceScope, TServiceScope) Create(params object?[] parameters)
|
||||||
{
|
{
|
||||||
if (serviceScopeFactory.CreateScope() is IServiceScope serviceScope)
|
if (serviceScopeFactory.CreateScope() is IServiceScope serviceScope)
|
||||||
{
|
{
|
||||||
if (serviceScope.ServiceProvider.GetService<IServiceFactory>() is IServiceFactory factory)
|
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);
|
cache.Add(service, serviceScope);
|
||||||
|
foreach (IInitializationScoped initializationScoped in serviceScope.ServiceProvider.GetServices<IInitializationScoped>())
|
||||||
|
{
|
||||||
|
initializationScoped.Initialize();
|
||||||
|
}
|
||||||
|
|
||||||
return (serviceScope, service);
|
return (serviceScope, service);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ public class ContentTemplate :
|
|||||||
DataTemplateSelector,
|
DataTemplateSelector,
|
||||||
IContentTemplate
|
IContentTemplate
|
||||||
{
|
{
|
||||||
|
private readonly Dictionary<string, DataTemplate> _cache = [];
|
||||||
|
|
||||||
protected override DataTemplate? SelectTemplateCore(object item)
|
protected override DataTemplate? SelectTemplateCore(object item)
|
||||||
{
|
{
|
||||||
if (item is IObservableViewModel observableViewModel)
|
if (item is IObservableViewModel observableViewModel)
|
||||||
@@ -17,10 +19,19 @@ public class ContentTemplate :
|
|||||||
if (observableViewModel.Provider is IServiceProvider provider)
|
if (observableViewModel.Provider is IServiceProvider provider)
|
||||||
{
|
{
|
||||||
Type itemType = item.GetType();
|
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)
|
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,
|
protected override DataTemplate? SelectTemplateCore(object item,
|
||||||
DependencyObject container)
|
DependencyObject container) => SelectTemplateCore(item);
|
||||||
{
|
|
||||||
return SelectTemplateCore(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static DataTemplate CreateDataTemplate(IContentTemplateDescriptor descriptor)
|
private static DataTemplate CreateDataTemplate(IContentTemplateDescriptor descriptor)
|
||||||
{
|
{
|
||||||
@@ -44,4 +52,4 @@ public class ContentTemplate :
|
|||||||
|
|
||||||
return (DataTemplate)XamlReader.Load(xamlString);
|
return (DataTemplate)XamlReader.Load(xamlString);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user