This commit is contained in:
2025-10-27 17:39:18 -04:00
commit 31f723bea4
1579 changed files with 642409 additions and 0 deletions

62
Prefab/Config.cs Normal file
View File

@@ -0,0 +1,62 @@
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;
}
}