using System.Diagnostics.CodeAnalysis;
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using Svrnty.CQRS.Abstractions;
using Svrnty.CQRS.Configuration;
namespace Svrnty.CQRS.FluentValidation;
///
/// Extension methods for CqrsBuilder to support FluentValidation
///
public static class CqrsBuilderExtensions
{
///
/// Adds a command handler with FluentValidation validator to the CQRS pipeline
///
public static CqrsBuilder AddCommand(
this CqrsBuilder builder)
where TCommand : class
where TCommandHandler : class, ICommandHandler
where TValidator : class, IValidator
{
// Add the command handler
builder.AddCommand();
// Add the validator
builder.Services.AddTransient, TValidator>();
return builder;
}
///
/// Adds a command handler with result and FluentValidation validator to the CQRS pipeline
///
public static CqrsBuilder AddCommand(
this CqrsBuilder builder)
where TCommand : class
where TCommandHandler : class, ICommandHandler
where TValidator : class, IValidator
{
// Add the command handler
builder.AddCommand();
// Add the validator
builder.Services.AddTransient, TValidator>();
return builder;
}
///
/// Adds a query handler with FluentValidation validator to the CQRS pipeline
///
public static CqrsBuilder AddQuery(
this CqrsBuilder builder)
where TQuery : class
where TQueryHandler : class, IQueryHandler
where TValidator : class, IValidator
{
// Add the query handler
builder.AddQuery();
// Add the validator
builder.Services.AddTransient, TValidator>();
return builder;
}
}