| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using NexMediator.Abstractions.Interfaces; |
| | | 5 | | using NexMediator.Core; |
| | | 6 | | using NexMediator.Pipeline.Behaviors; |
| | | 7 | | |
| | | 8 | | |
| | | 9 | | namespace NexMediator.Extensions; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Extension method to register NexMediator and its dependencies |
| | | 13 | | /// </summary> |
| | | 14 | | public static class ServiceCollectionExtensions |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Registers all NexMediator services in the DI container |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="services">The service collection to add to</param> |
| | | 20 | | /// <param name="configure">A delegate to configure pipeline behaviors</param> |
| | | 21 | | public static IServiceCollection AddNexMediator(this IServiceCollection services, Action<NexMediatorOptions>? config |
| | 18 | 22 | | { |
| | | 23 | | // Create configuration options with access to the service collection |
| | 18 | 24 | | var options = new NexMediatorOptions(services); |
| | 18 | 25 | | configure?.Invoke(options); |
| | | 26 | | |
| | | 27 | | //Minimal logging infra for behaviors only |
| | 18 | 28 | | if (options.HasBehavior(typeof(LoggingBehavior<,>))) |
| | 0 | 29 | | { |
| | 0 | 30 | | services.TryAddSingleton<ILoggerFactory, LoggerFactory>(); |
| | 0 | 31 | | services.TryAddTransient(typeof(ILogger<>), typeof(Logger<>)); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | // Register mediator core components |
| | 18 | 35 | | services.AddSingleton(options); |
| | 18 | 36 | | services.AddSingleton<INexMediator, DefaultNexMediator>(); |
| | | 37 | | |
| | | 38 | | // Automatically register request handlers, notification handlers, and validators |
| | 36 | 39 | | services.Scan(scan => scan.FromApplicationDependencies() |
| | 36 | 40 | | |
| | 36 | 41 | | // Register all request handlers |
| | 18 | 42 | | .AddClasses(c => c.AssignableTo(typeof(INexRequestHandler<,>))) |
| | 36 | 43 | | .AsImplementedInterfaces() |
| | 36 | 44 | | .WithTransientLifetime() |
| | 36 | 45 | | |
| | 36 | 46 | | // Register all notification handlers |
| | 18 | 47 | | .AddClasses(c => c.AssignableTo(typeof(INexNotificationHandler<>))) |
| | 36 | 48 | | .AsImplementedInterfaces() |
| | 36 | 49 | | .WithTransientLifetime() |
| | 36 | 50 | | |
| | 36 | 51 | | // Register all stream handlers |
| | 18 | 52 | | .AddClasses(c => c.AssignableTo(typeof(INexStreamRequestHandler<,>))) |
| | 36 | 53 | | .AsImplementedInterfaces() |
| | 36 | 54 | | .WithTransientLifetime() |
| | 36 | 55 | | |
| | 36 | 56 | | // Register all FluentValidation validators |
| | 18 | 57 | | .AddClasses(c => c.AssignableTo(typeof(FluentValidation.IValidator<>))) |
| | 36 | 58 | | .AsImplementedInterfaces() |
| | 36 | 59 | | .WithTransientLifetime() |
| | 18 | 60 | | ); |
| | | 61 | | |
| | 18 | 62 | | return services; |
| | 18 | 63 | | } |
| | | 64 | | } |