using Microsoft.Extensions.DependencyInjection;
using Svrnty.CQRS.Abstractions;
using Svrnty.CQRS.Discovery;
namespace Svrnty.CQRS.Configuration;
///
/// Builder for configuring CQRS services
///
public class CqrsBuilder
{
///
/// Gets the service collection for manual service registration
///
public IServiceCollection Services { get; }
///
/// Gets the CQRS configuration object (used by extension packages)
///
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);
}
///
/// Completes the builder configuration
///
internal void Build()
{
// Configuration is now handled by extension methods in respective packages
}
///
/// Adds a command handler to the CQRS pipeline
///
public CqrsBuilder AddCommand()
where TCommand : class
where TCommandHandler : class, ICommandHandler
{
Services.AddCommand();
return this;
}
///
/// Adds a command handler with result to the CQRS pipeline
///
public CqrsBuilder AddCommand()
where TCommand : class
where TCommandHandler : class, ICommandHandler
{
Services.AddCommand();
return this;
}
///
/// Adds a query handler to the CQRS pipeline
///
public CqrsBuilder AddQuery()
where TQuery : class
where TQueryHandler : class, IQueryHandler
{
Services.AddQuery();
return this;
}
}