dotnet-cqrs/OpenHarbor.CQRS.Abstractions/ServiceCollectionExtensions.cs

50 lines
2.2 KiB
C#

using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.DependencyInjection;
using OpenHarbor.CQRS.Abstractions.Discovery;
namespace OpenHarbor.CQRS.Abstractions;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddQuery<TQuery, TQueryResult, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TQueryHandler>(this IServiceCollection services)
where TQuery : class
where TQueryHandler : class, IQueryHandler<TQuery, TQueryResult>
{
// add handler to DI.
services.AddTransient<IQueryHandler<TQuery, TQueryResult>, TQueryHandler>();
// add for discovery purposes.
var queryMeta = new QueryMeta(typeof(TQuery), typeof(IQueryHandler<TQuery, TQueryResult>), typeof(TQueryResult));
services.AddSingleton<IQueryMeta>(queryMeta);
return services;
}
public static IServiceCollection AddCommand<TCommand, TCommandResult, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TCommandHandler>(this IServiceCollection services)
where TCommand : class
where TCommandHandler : class, ICommandHandler<TCommand, TCommandResult>
{
// add handler to DI.
services.AddTransient<ICommandHandler<TCommand, TCommandResult>, TCommandHandler>();
// add for discovery purposes.
var commandMeta = new CommandMeta(typeof(TCommand), typeof(ICommandHandler<TCommand>), typeof(TCommandResult));
services.AddSingleton<ICommandMeta>(commandMeta);
return services;
}
public static IServiceCollection AddCommand<TCommand, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TCommandHandler>(this IServiceCollection services)
where TCommand : class
where TCommandHandler : class, ICommandHandler<TCommand>
{
// add handler to DI.
services.AddTransient<ICommandHandler<TCommand>, TCommandHandler>();
// add for discovery purposes.
var commandMeta = new CommandMeta(typeof(TCommand), typeof(ICommandHandler<TCommand>));
services.AddSingleton<ICommandMeta>(commandMeta);
return services;
}
}