diff --git a/src/Carter/CarterConfigurator.cs b/src/Carter/CarterConfigurator.cs index be785e1..8840ffc 100644 --- a/src/Carter/CarterConfigurator.cs +++ b/src/Carter/CarterConfigurator.cs @@ -2,6 +2,7 @@ namespace Carter; using System; using System.Collections.Generic; +using System.Reflection; using FluentValidation; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -16,16 +17,21 @@ internal CarterConfigurator() this.ModuleTypes = new List(); this.ValidatorTypes = new List(); this.ResponseNegotiatorTypes = new List(); - this.ValidatorServiceLifetime = ServiceLifetime.Singleton; + this.DefaultValidatorServiceLifetime = ServiceLifetime.Singleton; + this.ValidatorServiceLifetimeFactory = (validatorType) => + validatorType.GetCustomAttribute()?.Lifetime ?? + this.DefaultValidatorServiceLifetime; } internal bool ExcludeValidators; internal bool ExcludeModules; - + internal bool ExcludeResponseNegotiators; - internal ServiceLifetime ValidatorServiceLifetime; + internal ServiceLifetime DefaultValidatorServiceLifetime; + + internal Func ValidatorServiceLifetimeFactory; internal List ModuleTypes { get; } @@ -131,7 +137,7 @@ public CarterConfigurator WithEmptyValidators() this.ExcludeValidators = true; return this; } - + /// /// Do not register any modules /// @@ -141,7 +147,7 @@ public CarterConfigurator WithEmptyModules() this.ExcludeModules = true; return this; } - + /// /// Do not register any response negotiators /// @@ -153,13 +159,24 @@ public CarterConfigurator WithEmptyResponseNegotiators() } /// - /// Define the lifetime of the validator + /// Define the default lifetime of the validator /// Default: Singleton /// /// - public CarterConfigurator WithValidatorLifetime(ServiceLifetime serviceLifetime) + public CarterConfigurator WithDefaultValidatorLifetime(ServiceLifetime serviceLifetime) + { + this.DefaultValidatorServiceLifetime = serviceLifetime; + this.ValidatorServiceLifetimeFactory = (_) => serviceLifetime; + return this; + } + + /// + /// Define the validator lifetime factory for custom validator lifetimes based on their type + /// + public CarterConfigurator WithValidatorServiceLifetimeFactory( + Func validatorServiceLifetimeFactory) { - this.ValidatorServiceLifetime = serviceLifetime; + this.ValidatorServiceLifetimeFactory = validatorServiceLifetimeFactory; return this; } } diff --git a/src/Carter/CarterExtensions.cs b/src/Carter/CarterExtensions.cs index 105303a..0ebcc45 100644 --- a/src/Carter/CarterExtensions.cs +++ b/src/Carter/CarterExtensions.cs @@ -175,26 +175,33 @@ private static void WireupCarter(this IServiceCollection services, services.AddSingleton(carterConfigurator); + ServiceLifetime validatorLocatorLifetime = ServiceLifetime.Singleton; foreach (var validator in validators) { + var validatorServiceLifetime = carterConfigurator.ValidatorServiceLifetimeFactory(validator); services.Add( new ServiceDescriptor( serviceType: typeof(IValidator), implementationType: validator, - lifetime: carterConfigurator.ValidatorServiceLifetime)); + lifetime: validatorServiceLifetime)); services.Add( new ServiceDescriptor( serviceType: validator, implementationType: validator, - lifetime: carterConfigurator.ValidatorServiceLifetime)); + lifetime: validatorServiceLifetime)); + + if (validatorServiceLifetime < validatorLocatorLifetime) + { + validatorLocatorLifetime = validatorServiceLifetime; + } } services.Add( new ServiceDescriptor( serviceType: typeof(IValidatorLocator), implementationType: typeof(DefaultValidatorLocator), - lifetime: carterConfigurator.ValidatorServiceLifetime)); + lifetime: validatorLocatorLifetime)); foreach (var newModule in newModules) { diff --git a/src/Carter/ValidatorLifetimeAttirubute.cs b/src/Carter/ValidatorLifetimeAttirubute.cs new file mode 100644 index 0000000..07f9d33 --- /dev/null +++ b/src/Carter/ValidatorLifetimeAttirubute.cs @@ -0,0 +1,10 @@ +namespace Carter; + +using System; +using Microsoft.Extensions.DependencyInjection; + +[AttributeUsage(AttributeTargets.Class)] +public sealed class ValidatorLifetimeAttribute(ServiceLifetime lifetime) : Attribute +{ + public ServiceLifetime Lifetime { get; } = lifetime; +}