63 lines
2.9 KiB
C#
63 lines
2.9 KiB
C#
using FluentValidation;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Prefab.Handler;
|
|
using Prefab.Handler.Decorators;
|
|
|
|
namespace Prefab;
|
|
|
|
/// <summary>
|
|
/// Provides extension methods for registering core Prefab services with the dependency injection container.
|
|
/// </summary>
|
|
public static class Config
|
|
{
|
|
/// <summary>
|
|
/// Adds Prefab handler pipeline services and decorators to the specified service collection.
|
|
/// </summary>
|
|
/// <remarks>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.</remarks>
|
|
/// <param name="services">The service collection to which the Prefab services will be added. Cannot be null.</param>
|
|
/// <param name="configuration">The application configuration used to configure Prefab services. Cannot be null.</param>
|
|
/// <returns>The same instance of <see cref="IServiceCollection"/> that was provided, to support method chaining.</returns>
|
|
public static IServiceCollection AddPrefab(this IServiceCollection services, IConfiguration configuration)
|
|
{
|
|
services.AddHttpContextAccessor();
|
|
services.AddScoped<IHandlerContextAccessor, HandlerContextAccessor>();
|
|
|
|
// Default decorators for handler pipeline. Place in order of execution, same way as .NET Core middleware does.
|
|
services.AddScoped<IHandlerDecorator, HandlerContextDecorator>();
|
|
services.AddScoped<IHandlerDecorator, ValidationDecorator>();
|
|
services.AddScoped<IHandlerDecorator, LoggingDecorator>();
|
|
|
|
services.AddScoped<HandlerInvoker>();
|
|
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Scans Prefab assemblies for handler implementations and registers them with the dependency injection container.
|
|
/// </summary>
|
|
/// <remarks>Call this after all Prefab modules are loaded so Scrutor can discover handlers defined within them.</remarks>
|
|
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;
|
|
}
|
|
}
|
|
|
|
|