diff --git a/src/CommonFramework.DependencyInjection/ServiceCollectionValidationExtensions.cs b/src/CommonFramework.DependencyInjection/ServiceCollectionValidationExtensions.cs index d53988b..e4550f0 100644 --- a/src/CommonFramework.DependencyInjection/ServiceCollectionValidationExtensions.cs +++ b/src/CommonFramework.DependencyInjection/ServiceCollectionValidationExtensions.cs @@ -8,69 +8,74 @@ namespace CommonFramework.DependencyInjection; /// public static class ServiceCollectionValidationExtensions { - /// - /// Registers a validator of type with the service collection. - /// - /// The type of the validator to register. Must implement and have a parameterless constructor. /// The service collection to add the validator to. - /// The original for chaining. - public static IServiceCollection AddValidator(this IServiceCollection services) - where TValidator : IServiceCollectionValidator, new() + extension(IServiceCollection services) { - return services.AddValidator(new TValidator()); - } - - /// - /// Registers an instance of with the service collection. - /// - /// The service collection to add the validator to. - /// The validator instance to register. - /// The original for chaining. - public static IServiceCollection AddValidator(this IServiceCollection services, IServiceCollectionValidator validator) - { - return services.AddSingleton(validator); - } + /// + /// Registers a validator of type with the service collection. + /// + /// The type of the validator to register. Must implement and have a parameterless constructor. + /// The original for chaining. + public IServiceCollection AddValidator() + where TValidator : IServiceCollectionValidator, new() + { + return services.AddValidator(new TValidator()); + } - /// - /// Performs manual validation of the using all registered validators. - /// - /// The service collection to validate. - /// Optional parameter providing additional options for validation. Can be null. - /// The original for chaining. - /// - /// Thrown if any of the registered validators report errors. The exception message contains all aggregated validation errors. - /// - /// - /// This method is a temporary workaround. The default DI container does not natively support - /// collecting validation errors at build time. Ideally, validators would be invoked automatically - /// during BuildServiceProvider, and all errors would be aggregated into a single exception - /// thrown by the DI engine. - /// - public static IServiceCollection Validate(this IServiceCollection services, object? options = null) - { - var validationResult = services - .GetValidators() - .Select(validator => validator.Validate(services, options)) - .Aggregate(ValidationResult.Success, (v1, v2) => v1 + v2); + /// + /// Registers an instance of with the service collection. + /// + /// The validator instance to register. + /// The original for chaining. + public IServiceCollection AddValidator(IServiceCollectionValidator validator) + { + return services.AddSingleton(validator); + } - if (!validationResult.IsSuccess) + /// + /// Performs manual validation of the using all registered validators. + /// + /// Optional parameter providing additional options for validation. Can be null. + /// The original for chaining. + /// + /// Thrown if any of the registered validators report errors. The exception message contains all aggregated validation errors. + /// + /// + /// This method is a temporary workaround. The default DI container does not natively support + /// collecting validation errors at build time. Ideally, validators would be invoked automatically + /// during BuildServiceProvider, and all errors would be aggregated into a single exception + /// thrown by the DI engine. + /// + public IServiceCollection Validate(object? options = null) { - var message = string.Join(Environment.NewLine, validationResult.Errors); + var validationResult = services + .GetValidators() + .Select(validator => validator.Validate(services, options)) + .Aggregate(ValidationResult.Success, (v1, v2) => v1 + v2); - throw new InvalidOperationException(message); + if (!validationResult.IsSuccess) + { + var message = string.Join(Environment.NewLine, validationResult.Errors); + + throw new InvalidOperationException(message); + } + + return services; } - return services; - } + private IEnumerable GetValidators() + { + return - private static IEnumerable GetValidators(this IServiceCollection services) - { - return - from sd in services - where sd.Lifetime == ServiceLifetime.Singleton - && !sd.IsKeyedService - && sd.ServiceType == typeof(IServiceCollectionValidator) - && sd.ImplementationInstance != null - select (IServiceCollectionValidator)sd.ImplementationInstance; + from sd in services + + where sd is { Lifetime: ServiceLifetime.Singleton, IsKeyedService: false } && sd.ServiceType == typeof(IServiceCollectionValidator) + + let validator = sd.ImplementationInstance as IServiceCollectionValidator + + where validator != null + + select validator; + } } } \ No newline at end of file diff --git a/src/CommonFramework.VisualIdentitySource/IVisualIdentityInfoSource.cs b/src/CommonFramework.VisualIdentitySource/IVisualIdentityInfoSource.cs index 4b35ca4..215ca6f 100644 --- a/src/CommonFramework.VisualIdentitySource/IVisualIdentityInfoSource.cs +++ b/src/CommonFramework.VisualIdentitySource/IVisualIdentityInfoSource.cs @@ -5,4 +5,8 @@ public interface IVisualIdentityInfoSource VisualIdentityInfo GetVisualIdentityInfo(); VisualIdentityInfo? TryGetVisualIdentityInfo(); + + VisualIdentityInfo GetVisualIdentityInfo(Type domainObjectType); + + VisualIdentityInfo? TryGetVisualIdentityInfo(Type domainObjectType); } \ No newline at end of file diff --git a/src/CommonFramework.VisualIdentitySource/VisualIdentityInfoSource.cs b/src/CommonFramework.VisualIdentitySource/VisualIdentityInfoSource.cs index 0cb50f0..1dc0867 100644 --- a/src/CommonFramework.VisualIdentitySource/VisualIdentityInfoSource.cs +++ b/src/CommonFramework.VisualIdentitySource/VisualIdentityInfoSource.cs @@ -34,20 +34,32 @@ public class VisualIdentityInfoSource(IVisualIdentityPropertyExtractor propertyE } }).WithLock(); + public VisualIdentityInfo? TryGetVisualIdentityInfo() + { + return (VisualIdentityInfo?)this.TryGetVisualIdentityInfo(typeof(TDomainObject)); + } + public VisualIdentityInfo GetVisualIdentityInfo(Type domainObjectType) + { + return this.TryGetVisualIdentityInfo(domainObjectType) ?? throw this.GetMissedError(domainObjectType); + } - public VisualIdentityInfo? TryGetVisualIdentityInfo() - { - return (VisualIdentityInfo?)this.cache[typeof(TDomainObject)]; - } + public VisualIdentityInfo? TryGetVisualIdentityInfo(Type domainObjectType) + { + return this.cache[domainObjectType]; + } - public VisualIdentityInfo GetVisualIdentityInfo() + public VisualIdentityInfo GetVisualIdentityInfo() { - return this.TryGetVisualIdentityInfo() ?? - throw new Exception($"{nameof(VisualIdentityInfo)} for {typeof(TDomainObject).Name} not found"); + return this.TryGetVisualIdentityInfo() ?? throw this.GetMissedError(typeof(TDomainObject)); } - private static VisualIdentityInfo CreateVisualIdentityInfo(Expression> namePath) + private Exception GetMissedError(Type domainObjectType) + { + return new Exception($"{nameof(VisualIdentityInfo)} for {domainObjectType.Name} not found"); + } + + private static VisualIdentityInfo CreateVisualIdentityInfo(Expression> namePath) { return new VisualIdentityInfo(namePath); } diff --git a/src/__SolutionItems/CommonAssemblyInfo.cs b/src/__SolutionItems/CommonAssemblyInfo.cs index 189cb9a..171d298 100644 --- a/src/__SolutionItems/CommonAssemblyInfo.cs +++ b/src/__SolutionItems/CommonAssemblyInfo.cs @@ -3,7 +3,7 @@ [assembly: AssemblyProduct("CommonFramework")] [assembly: AssemblyCompany("IvAt")] -[assembly: AssemblyVersion("1.6.3.0")] +[assembly: AssemblyVersion("1.6.4.0")] [assembly: AssemblyInformationalVersion("changes at build")] #if DEBUG