Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/Carter/CarterConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,16 +17,21 @@ internal CarterConfigurator()
this.ModuleTypes = new List<Type>();
this.ValidatorTypes = new List<Type>();
this.ResponseNegotiatorTypes = new List<Type>();
this.ValidatorServiceLifetime = ServiceLifetime.Singleton;
this.DefaultValidatorServiceLifetime = ServiceLifetime.Singleton;
this.ValidatorServiceLifetimeFactory = (validatorType) =>
validatorType.GetCustomAttribute<ValidatorLifetimeAttribute>()?.Lifetime ??
this.DefaultValidatorServiceLifetime;
}

internal bool ExcludeValidators;

internal bool ExcludeModules;

internal bool ExcludeResponseNegotiators;

internal ServiceLifetime ValidatorServiceLifetime;
internal ServiceLifetime DefaultValidatorServiceLifetime;

internal Func<Type, ServiceLifetime> ValidatorServiceLifetimeFactory;

internal List<Type> ModuleTypes { get; }

Expand Down Expand Up @@ -131,7 +137,7 @@ public CarterConfigurator WithEmptyValidators()
this.ExcludeValidators = true;
return this;
}

/// <summary>
/// Do not register any modules
/// </summary>
Expand All @@ -141,7 +147,7 @@ public CarterConfigurator WithEmptyModules()
this.ExcludeModules = true;
return this;
}

/// <summary>
/// Do not register any response negotiators
/// </summary>
Expand All @@ -153,13 +159,24 @@ public CarterConfigurator WithEmptyResponseNegotiators()
}

/// <summary>
/// Define the lifetime of the validator
/// Define the default lifetime of the validator
/// Default: Singleton
/// </summary>
/// <returns></returns>
public CarterConfigurator WithValidatorLifetime(ServiceLifetime serviceLifetime)
public CarterConfigurator WithDefaultValidatorLifetime(ServiceLifetime serviceLifetime)
{
this.DefaultValidatorServiceLifetime = serviceLifetime;
this.ValidatorServiceLifetimeFactory = (_) => serviceLifetime;
return this;
}

/// <summary>
/// Define the validator lifetime factory for custom validator lifetimes based on their type
/// </summary>
public CarterConfigurator WithValidatorServiceLifetimeFactory(
Func<Type, ServiceLifetime> validatorServiceLifetimeFactory)
{
this.ValidatorServiceLifetime = serviceLifetime;
this.ValidatorServiceLifetimeFactory = validatorServiceLifetimeFactory;
return this;
}
}
13 changes: 10 additions & 3 deletions src/Carter/CarterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

foreach (var carterModuleInterface in builder.ServiceProvider.GetServices<ICarterModule>())
{
if (carterModuleInterface is CarterModule carterModule)

Check warning on line 33 in src/Carter/CarterExtensions.cs

View workflow job for this annotation

GitHub Actions / Github Actions Build

'CarterModule' is obsolete: 'CarterModule will be removed in the next version. Please migrate to ICarterModule.'

Check warning on line 33 in src/Carter/CarterExtensions.cs

View workflow job for this annotation

GitHub Actions / Github Actions Build

'CarterModule' is obsolete: 'CarterModule will be removed in the next version. Please migrate to ICarterModule.'
{
var group = builder.MapGroup(carterModule.basePath);

Expand Down Expand Up @@ -175,26 +175,33 @@

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)
{
Expand Down
10 changes: 10 additions & 0 deletions src/Carter/ValidatorLifetimeAttirubute.cs
Original file line number Diff line number Diff line change
@@ -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;
}
Loading