70 lines
2.7 KiB
C#
70 lines
2.7 KiB
C#
#nullable enable
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using FluentValidation;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Svrnty.CQRS.Abstractions;
|
|
using Svrnty.CQRS.Configuration;
|
|
|
|
namespace Svrnty.CQRS.FluentValidation;
|
|
|
|
/// <summary>
|
|
/// Extension methods for CqrsBuilder to support FluentValidation
|
|
/// </summary>
|
|
public static class CqrsBuilderExtensions
|
|
{
|
|
/// <summary>
|
|
/// Adds a command handler with FluentValidation validator to the CQRS pipeline
|
|
/// </summary>
|
|
public static CqrsBuilder AddCommand<TCommand, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TCommandHandler, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TValidator>(
|
|
this CqrsBuilder builder)
|
|
where TCommand : class
|
|
where TCommandHandler : class, ICommandHandler<TCommand>
|
|
where TValidator : class, IValidator<TCommand>
|
|
{
|
|
// Add the command handler
|
|
builder.AddCommand<TCommand, TCommandHandler>();
|
|
|
|
// Add the validator
|
|
builder.Services.AddTransient<IValidator<TCommand>, TValidator>();
|
|
|
|
return builder;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a command handler with result and FluentValidation validator to the CQRS pipeline
|
|
/// </summary>
|
|
public static CqrsBuilder AddCommand<TCommand, TResult, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TCommandHandler, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TValidator>(
|
|
this CqrsBuilder builder)
|
|
where TCommand : class
|
|
where TCommandHandler : class, ICommandHandler<TCommand, TResult>
|
|
where TValidator : class, IValidator<TCommand>
|
|
{
|
|
// Add the command handler
|
|
builder.AddCommand<TCommand, TResult, TCommandHandler>();
|
|
|
|
// Add the validator
|
|
builder.Services.AddTransient<IValidator<TCommand>, TValidator>();
|
|
|
|
return builder;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a query handler with FluentValidation validator to the CQRS pipeline
|
|
/// </summary>
|
|
public static CqrsBuilder AddQuery<TQuery, TResult, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TQueryHandler, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TValidator>(
|
|
this CqrsBuilder builder)
|
|
where TQuery : class
|
|
where TQueryHandler : class, IQueryHandler<TQuery, TResult>
|
|
where TValidator : class, IValidator<TQuery>
|
|
{
|
|
// Add the query handler
|
|
builder.AddQuery<TQuery, TResult, TQueryHandler>();
|
|
|
|
// Add the validator
|
|
builder.Services.AddTransient<IValidator<TQuery>, TValidator>();
|
|
|
|
return builder;
|
|
}
|
|
}
|