dotnet-cqrs/Svrnty.CQRS/Configuration/CqrsBuilder.cs
2025-11-04 16:45:54 -05:00

76 lines
2.2 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using Svrnty.CQRS.Abstractions;
using Svrnty.CQRS.Discovery;
namespace Svrnty.CQRS.Configuration;
/// <summary>
/// Builder for configuring CQRS services
/// </summary>
public class CqrsBuilder
{
/// <summary>
/// Gets the service collection for manual service registration
/// </summary>
public IServiceCollection Services { get; }
/// <summary>
/// Gets the CQRS configuration object (used by extension packages)
/// </summary>
public CqrsConfiguration Configuration { get; }
internal CqrsBuilder(IServiceCollection services)
{
Services = services;
Configuration = new CqrsConfiguration();
// Add discovery services by default
services.AddDefaultCommandDiscovery();
services.AddDefaultQueryDiscovery();
// Register configuration as singleton so it can be accessed later
services.AddSingleton(Configuration);
}
/// <summary>
/// Completes the builder configuration
/// </summary>
internal void Build()
{
// Configuration is now handled by extension methods in respective packages
}
/// <summary>
/// Adds a command handler to the CQRS pipeline
/// </summary>
public CqrsBuilder AddCommand<TCommand, TCommandHandler>()
where TCommand : class
where TCommandHandler : class, ICommandHandler<TCommand>
{
Services.AddCommand<TCommand, TCommandHandler>();
return this;
}
/// <summary>
/// Adds a command handler with result to the CQRS pipeline
/// </summary>
public CqrsBuilder AddCommand<TCommand, TResult, TCommandHandler>()
where TCommand : class
where TCommandHandler : class, ICommandHandler<TCommand, TResult>
{
Services.AddCommand<TCommand, TResult, TCommandHandler>();
return this;
}
/// <summary>
/// Adds a query handler to the CQRS pipeline
/// </summary>
public CqrsBuilder AddQuery<TQuery, TResult, TQueryHandler>()
where TQuery : class
where TQueryHandler : class, IQueryHandler<TQuery, TResult>
{
Services.AddQuery<TQuery, TResult, TQueryHandler>();
return this;
}
}