Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
sorensenmatias committed Mar 2, 2024
2 parents ae3466e + c19036b commit a3bf1b7
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public static IHostBuilder UseServiceProviderExtendedValidation(this IHostBuilde
return hostBuilder;
}

public static IHostBuilder UseServiceProviderExtendedValidation(this IHostBuilder hostBuilder, Action<ReportingBuilder> configureReporting)
public static IHostBuilder UseServiceProviderExtendedValidation(this IHostBuilder hostBuilder, Action<ReportConfigurer> configureReporting)
{
var reportingBuilder = new ReportingBuilder();
var reportingBuilder = new ReportConfigurer();
configureReporting(reportingBuilder);
hostBuilder.UseServiceProviderFactory(new ExtendedValidationServiceProviderFactory(new DefaultServiceProviderFactory(), reportingBuilder));
return hostBuilder;
Expand All @@ -28,10 +28,10 @@ public static HostApplicationBuilder ConfigureContainerWithServiceProviderExtend
}

public static HostApplicationBuilder ConfigureContainerWithServiceProviderExtendedValidation(this HostApplicationBuilder hostApplicationBuilder,
Action<ReportingBuilder> configureReporting,
Action<ReportConfigurer> configureReporting,
Action<IServiceCollection>? configure = null)
{
var reportingBuilder = new ReportingBuilder();
var reportingBuilder = new ReportConfigurer();
configureReporting(reportingBuilder);
hostApplicationBuilder.ConfigureContainer(new ExtendedValidationServiceProviderFactory(new DefaultServiceProviderFactory(), reportingBuilder), configure);
return hostApplicationBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace ServiceProviderValidationExtensions.Hosting.Internal;
internal sealed class ExtendedValidationServiceProviderFactory : IServiceProviderFactory<IServiceCollection>
{
private readonly DefaultServiceProviderFactory _defaultServiceProviderFactory;
private readonly ReportingBuilder? _reportingBuilder;
private readonly ReportConfigurer? _reportingBuilder;

public ExtendedValidationServiceProviderFactory(DefaultServiceProviderFactory defaultServiceProviderFactory, ReportingBuilder? reportingBuilder)
public ExtendedValidationServiceProviderFactory(DefaultServiceProviderFactory defaultServiceProviderFactory, ReportConfigurer? reportingBuilder)
{
_defaultServiceProviderFactory = defaultServiceProviderFactory;
_reportingBuilder = reportingBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ServiceProviderValidationExtensions;

public interface IReportingBuilder
public interface IReportConfigurer
{
void Report(IServiceCollection serviceCollection);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

namespace ServiceProviderValidationExtensions;

public sealed class ReportingBuilder : IReportingBuilder
public sealed class ReportConfigurer : IReportConfigurer
{
private readonly IList<Action<DuplicateServiceContent>> _duplicateServiceActions = new List<Action<DuplicateServiceContent>>();
private Action<DuplicateServiceContent>? _duplicateServiceAction;
private readonly IList<Type> _duplicateServiceExclusions = new List<Type>();

public ReportingBuilder OnDuplicateService(Action<DuplicateServiceContent> action,
public ReportConfigurer OnDuplicateService(Action<DuplicateServiceContent> action,
Action<ReportingBuilderDuplicateServiceConfiguration>? configuration = null)
{
_duplicateServiceActions.Add(action);
_duplicateServiceAction = action;
if (configuration is not null)
{
configuration(new ReportingBuilderDuplicateServiceConfiguration(this));
Expand All @@ -22,22 +22,22 @@ public ReportingBuilder OnDuplicateService(Action<DuplicateServiceContent> actio

public sealed class ReportingBuilderDuplicateServiceConfiguration
{
private readonly ReportingBuilder _reportingBuilder;
private readonly ReportConfigurer _reportConfigurer;

internal ReportingBuilderDuplicateServiceConfiguration(ReportingBuilder reportingBuilder)
internal ReportingBuilderDuplicateServiceConfiguration(ReportConfigurer reportConfigurer)
{
_reportingBuilder = reportingBuilder;
_reportConfigurer = reportConfigurer;
}

public ReportingBuilderDuplicateServiceConfiguration Except<T>()
{
_reportingBuilder._duplicateServiceExclusions.Add(typeof(T));
_reportConfigurer._duplicateServiceExclusions.Add(typeof(T));
return this;
}

public ReportingBuilderDuplicateServiceConfiguration Except(Type type)
{
_reportingBuilder._duplicateServiceExclusions.Add(type);
_reportConfigurer._duplicateServiceExclusions.Add(type);
return this;
}
}
Expand All @@ -49,7 +49,7 @@ public void Report(IServiceCollection serviceCollection)

private void ReportDuplicateServices(IServiceCollection serviceCollection)
{
if (!_duplicateServiceActions.Any())
if (_duplicateServiceAction is null)
{
return;
}
Expand All @@ -64,12 +64,14 @@ private void ReportDuplicateServices(IServiceCollection serviceCollection)
return false;
}

if (duplicateServiceExclusion.IsGenericType)
if (!duplicateServiceExclusion.IsGenericType)
{
if (sd.ServiceType.IsDerivedFromGenericParent(duplicateServiceExclusion))
{
return false;
}
continue;
}

if (sd.ServiceType.IsDerivedFromGenericParent(duplicateServiceExclusion))
{
return false;
}
}

Expand All @@ -84,10 +86,8 @@ private void ReportDuplicateServices(IServiceCollection serviceCollection)
.Select(t => new TypeInfo(t))
.ToList();

foreach (var action in _duplicateServiceActions)
{
action(new DuplicateServiceContent(new TypeInfo(groupItem.Key), implementationTypes));
}
var duplicateServiceContent = new DuplicateServiceContent(new TypeInfo(groupItem.Key), implementationTypes);
_duplicateServiceAction(duplicateServiceContent);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,18 @@ public static IServiceCollection AddTransient<TService, TImplementation>(this IS
return services;
}

public static ServiceProvider BuildServiceProviderWithValidation(this IServiceCollection serviceCollection, IReportingBuilder? reportingBuilder = null)
public static ServiceProvider BuildServiceProviderWithValidation(this IServiceCollection serviceCollection, IReportConfigurer? reportingBuilder = null)
{
var serviceProvider = ReportAndBuildAndValidate(serviceCollection, reportingBuilder, null);
return serviceProvider;
}

public static ServiceProvider BuildServiceProviderWithValidation(this IServiceCollection serviceCollection, ServiceProviderOptions options, ReportingBuilder? reportingBuilder = null)
public static ServiceProvider BuildServiceProviderWithValidation(this IServiceCollection serviceCollection, ServiceProviderOptions options, ReportConfigurer? reportingBuilder = null)
{
return ReportAndBuildAndValidate(serviceCollection, reportingBuilder, options);
}

internal static ServiceProvider ReportAndBuildAndValidate(IServiceCollection serviceCollection, IReportingBuilder? reportingBuilder, ServiceProviderOptions? options)
internal static ServiceProvider ReportAndBuildAndValidate(IServiceCollection serviceCollection, IReportConfigurer? reportingBuilder, ServiceProviderOptions? options)
{
if (reportingBuilder is not null)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Hosting/HostFullReportingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void DefaultBuilder()
{
var duplicateServiceReports = new List<string>();

void ConfigureReporting(ReportingBuilder rb)
void ConfigureReporting(ReportConfigurer rb)
{
rb.OnDuplicateService(dsc => duplicateServiceReports.Add($"{dsc.ServiceType.DisplayName} is registered {dsc.ImplementationTypes.Count} times"),
c => c.Except(typeof(IConfigureOptions<>))
Expand Down
4 changes: 2 additions & 2 deletions tests/ReportingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void ServiceRegisteredTwice()

var duplicateServiceOutput = new List<string>();

var reportingBuilder = new ReportingBuilder().OnDuplicateService(ds => duplicateServiceOutput.Add(
var reportingBuilder = new ReportConfigurer().OnDuplicateService(ds => duplicateServiceOutput.Add(
$"{ds.ServiceType.DisplayName} is registered {ds.ImplementationTypes.Count} times"));

serviceCollection.BuildServiceProviderWithValidation(reportingBuilder);
Expand All @@ -34,7 +34,7 @@ public void ServiceRegisteredTwiceOnceUsingLambda()

var duplicateServiceOutput = new List<string>();

var reportingBuilder = new ReportingBuilder().OnDuplicateService(ds => duplicateServiceOutput.Add(
var reportingBuilder = new ReportConfigurer().OnDuplicateService(ds => duplicateServiceOutput.Add(
$"{ds.ServiceType.DisplayName} is registered {ds.ImplementationTypes.Count} times"));

serviceCollection.BuildServiceProviderWithValidation(reportingBuilder);
Expand Down

0 comments on commit a3bf1b7

Please sign in to comment.