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

43 lines
1.7 KiB
C#
Raw Permalink Normal View History

using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using OpenHarbor.CQRS.Abstractions;
namespace OpenHarbor.CQRS.FluentValidation;
public static class ServiceCollectionExtensions
{
private static IServiceCollection AddFluentValidator<T, TValidator>(this IServiceCollection services)
where TValidator : class, IValidator<T>
{
services.AddTransient<IValidator<T>, TValidator>();
return services;
}
public static IServiceCollection AddCommand<TCommand, TCommandHandler, TValidator>(this IServiceCollection services)
where TCommand : class
where TCommandHandler : class, ICommandHandler<TCommand>
where TValidator : class, IValidator<TCommand>
{
return services.AddCommand<TCommand, TCommandHandler>()
.AddFluentValidator<TCommand, TValidator>();
}
public static IServiceCollection AddCommand<TCommand, TCommandResult, TCommandHandler, TValidator>(this IServiceCollection services)
where TCommand : class
where TCommandHandler : class, ICommandHandler<TCommand, TCommandResult>
where TValidator : class, IValidator<TCommand>
{
return services.AddCommand<TCommand, TCommandResult, TCommandHandler>()
.AddFluentValidator<TCommand, TValidator>();
}
public static IServiceCollection AddQuery<TQuery, TQueryResult, TQueryHandler, TValidator>(this IServiceCollection services)
where TQuery : class
where TQueryHandler : class, IQueryHandler<TQuery, TQueryResult>
where TValidator : class, IValidator<TQuery>
{
services.AddQuery<TQuery, TQueryResult, TQueryHandler>()
.AddFluentValidator<TQuery, TValidator>();
return services;
}
}