diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 195fdd9..28e5417 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -1,39 +1,42 @@ - - ServiceProviderValidationExtensions.Tests - ServiceProviderValidationExtensions.Tests - net7.0;net8.0 - 11.0 - enable - enable + + ServiceProviderValidationExtensions.Tests + ServiceProviderValidationExtensions.Tests + net7.0;net8.0 + 11.0 + enable + enable - false - true - + false + true + - - + + - - - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + - - - + + + diff --git a/src/ServiceProviderValidationExtensions.Hosting/ServiceProviderValidationExtensions.Hosting.csproj b/src/ServiceProviderValidationExtensions.Hosting/ServiceProviderValidationExtensions.Hosting.csproj index 5e59398..8f57f8f 100644 --- a/src/ServiceProviderValidationExtensions.Hosting/ServiceProviderValidationExtensions.Hosting.csproj +++ b/src/ServiceProviderValidationExtensions.Hosting/ServiceProviderValidationExtensions.Hosting.csproj @@ -1,11 +1,11 @@ - - - + + + - - - + + + diff --git a/src/ServiceProviderValidationExtensions/Internal/Persistence.cs b/src/ServiceProviderValidationExtensions/Internal/Persistence.cs index 3fdfe64..cbee4e5 100644 --- a/src/ServiceProviderValidationExtensions/Internal/Persistence.cs +++ b/src/ServiceProviderValidationExtensions/Internal/Persistence.cs @@ -42,7 +42,7 @@ private static ValidationRegistrations CreateAndPersistInstance(IServiceCollecti return services[0].ImplementationInstance as ValidationRegistrations; } - + private static (ValidationRegistrations validationRegistrations, ServiceDescriptor serviceDescriptor)? SearchForValidationRegistrations(IServiceCollection services) { var serviceDescriptor = services.FirstOrDefault(sd => sd.ImplementationInstance is ValidationRegistrations); diff --git a/src/ServiceProviderValidationExtensions/Internal/TypeExtensions.cs b/src/ServiceProviderValidationExtensions/Internal/TypeExtensions.cs index 3a2cfed..e0e3759 100644 --- a/src/ServiceProviderValidationExtensions/Internal/TypeExtensions.cs +++ b/src/ServiceProviderValidationExtensions/Internal/TypeExtensions.cs @@ -8,10 +8,12 @@ public static bool IsDerivedFromGenericParent(this Type? type, Type parentType) { throw new ArgumentException("type must be generic", nameof(parentType)); } + if (type == null || type == typeof(object)) { return false; } + if (type.IsGenericType && type.GetGenericTypeDefinition() == parentType) { return true; diff --git a/src/ServiceProviderValidationExtensions/ReportConfigurer.cs b/src/ServiceProviderValidationExtensions/ReportConfigurer.cs index 7156d00..59636f5 100644 --- a/src/ServiceProviderValidationExtensions/ReportConfigurer.cs +++ b/src/ServiceProviderValidationExtensions/ReportConfigurer.cs @@ -5,9 +5,14 @@ namespace ServiceProviderValidationExtensions; public sealed class ReportConfigurer : IReportConfigurer { - private Action? _duplicateServiceAction; private readonly IList _duplicateServiceExclusions = new List(); - + private Action? _duplicateServiceAction; + + public void Report(IServiceCollection serviceCollection) + { + ReportDuplicateServices(serviceCollection); + } + public ReportConfigurer OnDuplicateService(Action action, Action? configuration = null) { @@ -16,35 +21,8 @@ public ReportConfigurer OnDuplicateService(Action actio { configuration(new ReportingDuplicateServiceConfiguration(this)); } - - return this; - } - - public sealed class ReportingDuplicateServiceConfiguration - { - private readonly ReportConfigurer _reportConfigurer; - - internal ReportingDuplicateServiceConfiguration(ReportConfigurer reportConfigurer) - { - _reportConfigurer = reportConfigurer; - } - - public ReportingDuplicateServiceConfiguration Except() - { - _reportConfigurer._duplicateServiceExclusions.Add(typeof(T)); - return this; - } - - public ReportingDuplicateServiceConfiguration Except(Type type) - { - _reportConfigurer._duplicateServiceExclusions.Add(type); - return this; - } - } - public void Report(IServiceCollection serviceCollection) - { - ReportDuplicateServices(serviceCollection); + return this; } private void ReportDuplicateServices(IServiceCollection serviceCollection) @@ -91,6 +69,28 @@ private void ReportDuplicateServices(IServiceCollection serviceCollection) } } + public sealed class ReportingDuplicateServiceConfiguration + { + private readonly ReportConfigurer _reportConfigurer; + + internal ReportingDuplicateServiceConfiguration(ReportConfigurer reportConfigurer) + { + _reportConfigurer = reportConfigurer; + } + + public ReportingDuplicateServiceConfiguration Except() + { + _reportConfigurer._duplicateServiceExclusions.Add(typeof(T)); + return this; + } + + public ReportingDuplicateServiceConfiguration Except(Type type) + { + _reportConfigurer._duplicateServiceExclusions.Add(type); + return this; + } + } + public sealed class DuplicateServiceContent { public DuplicateServiceContent(TypeInfo serviceType, IReadOnlyCollection implementationTypes) diff --git a/src/ServiceProviderValidationExtensions/ServiceCollectionValidationExtensions.cs b/src/ServiceProviderValidationExtensions/ServiceCollectionValidationExtensions.cs index 8e1b42d..24cfa37 100644 --- a/src/ServiceProviderValidationExtensions/ServiceCollectionValidationExtensions.cs +++ b/src/ServiceProviderValidationExtensions/ServiceCollectionValidationExtensions.cs @@ -171,7 +171,7 @@ public static void Validate(this IServiceProvider serviceProvider) } /// - /// Marks the as exclusive (only one registration can exist in the ServiceProvider). + /// Marks the as exclusive (only one registration can exist in the ServiceProvider). /// public static IServiceCollection SetExclusiveService(this IServiceCollection services) where TService : class @@ -197,7 +197,8 @@ private static void RegisterExclusiveService(this IServiceCollection s } /// - /// Marks the (,) combination as exclusive (only one registration can exist in the ServiceProvider). + /// Marks the (,) combination as exclusive (only one registration + /// can exist in the ServiceProvider). /// public static void SetExclusiveImplementation(this IServiceCollection services) where TImplementation : class diff --git a/src/ServiceProviderValidationExtensions/ServiceProviderValidationExtensions.csproj b/src/ServiceProviderValidationExtensions/ServiceProviderValidationExtensions.csproj index dc4e856..836dd50 100644 --- a/src/ServiceProviderValidationExtensions/ServiceProviderValidationExtensions.csproj +++ b/src/ServiceProviderValidationExtensions/ServiceProviderValidationExtensions.csproj @@ -1,8 +1,7 @@ - - - - - - + + + + +