Files
Toolkit2/Framework/Foundation/Extensions/IServiceCollectionExtensions.cs
T
2022-12-16 09:06:14 +00:00

53 lines
2.4 KiB
C#

using Mediator;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Toolkit.Framework.Foundation;
public static class IServiceCollectionExtensions
{
public static IServiceCollection AddHandler<TRequestHandler>(this IServiceCollection services) where TRequestHandler : notnull
{
if (typeof(TRequestHandler).GetInterface(typeof(IRequestHandler<,>).Name) is { } contract)
{
if (contract.GetGenericArguments() is { Length: 2 } arguments)
{
Type requestType = arguments[0];
Type responseType = arguments[1];
Type wrapperType = typeof(RequestClassHandlerWrapper<,>).MakeGenericType(requestType, responseType);
services.TryAdd(new ServiceDescriptor(typeof(TRequestHandler), typeof(TRequestHandler), ServiceLifetime.Transient));
services.Add(new ServiceDescriptor(wrapperType,
sp =>
{
return sp.GetService<IServiceFactory>()?.Create(wrapperType, sp.GetRequiredService<TRequestHandler>(), sp.GetServices(typeof(IPipelineBehavior<,>).MakeGenericType(requestType, responseType)))!;
},
ServiceLifetime.Transient
));
}
}
return services;
}
public static IServiceCollection AddFoundation(this IServiceCollection serviceCollection)
{
serviceCollection
.AddSingleton<IMediator, Mediator>()
.AddHandler<InitializeHandler>()
.AddSingleton<IServiceFactory>(provider => new ServiceFactory(provider.GetService, (instanceType, parameters) => ActivatorUtilities.CreateInstance(provider, instanceType, parameters!)))
.AddHandler<ServiceFactoryHandler>()
.AddSingleton<IInitialization, Initialization>(provider => new Initialization(() =>
{
return serviceCollection.Where(x => x.ServiceType.GetInterfaces()
.Contains(typeof(IInitializable)) || x.ServiceType == typeof(IInitializable))
.GroupBy(x => x.ServiceType)
.Select(x => x.First())
.SelectMany(x => provider.GetServices(x.ServiceType)
.Select(x => (IInitializable?)x)).ToList();
}));
return serviceCollection;
}
}