using FluentValidation;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Prefab.Handler;
using Prefab.Handler.Decorators;
namespace Prefab;
///
/// Provides extension methods for registering core Prefab services with the dependency injection container.
///
public static class Config
{
///
/// Adds Prefab handler pipeline services and decorators to the specified service collection.
///
/// This method registers the default handler pipeline decorators and invoker required for
/// Prefab. Decorators are added in the order of execution, similar to ASP.NET Core middleware.
/// The service collection to which the Prefab services will be added. Cannot be null.
/// The application configuration used to configure Prefab services. Cannot be null.
/// The same instance of that was provided, to support method chaining.
public static IServiceCollection AddPrefab(this IServiceCollection services, IConfiguration configuration)
{
services.AddHttpContextAccessor();
services.AddScoped();
// Default decorators for handler pipeline. Place in order of execution, same way as .NET Core middleware does.
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.AddScoped();
services.Scan(scan => scan
.FromApplicationDependencies(assembly => assembly.GetName().Name?.StartsWith(nameof(Prefab)) == true)
.AddClasses(classes => classes.AssignableTo(typeof(IValidator<>)), publicOnly: false)
.AsImplementedInterfaces()
.WithScopedLifetime());
return services;
}
///
/// Scans Prefab assemblies for handler implementations and registers them with the dependency injection container.
///
/// Call this after all Prefab modules are loaded so Scrutor can discover handlers defined within them.
public static IServiceCollection AddPrefabHandlers(this IServiceCollection services)
{
services.Scan(scan => scan
.FromApplicationDependencies(assembly => assembly.GetName().Name?.StartsWith(nameof(Prefab)) == true)
.AddClasses(classes => classes.AssignableTo(typeof(IHandler<,>)), publicOnly: false)
.AsImplementedInterfaces()
.WithScopedLifetime()
.AddClasses(classes => classes.AssignableTo(typeof(IHandler<>)), publicOnly: false)
.AsImplementedInterfaces()
.WithScopedLifetime());
return services;
}
}