Skip to content
This repository has been archived by the owner on Dec 2, 2023. It is now read-only.

Commit

Permalink
clean up (#2)
Browse files Browse the repository at this point in the history
John assisted in cleaning up the DI in Program.cs
  • Loading branch information
valincius authored Jan 18, 2022
1 parent c31ca9c commit 412a510
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 64 deletions.
2 changes: 0 additions & 2 deletions ExceptionAll.APIExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@

var app = builder.Build();

app.Services.AddExceptionAll();

// Configure the HTTP request pipeline.

app.UseSwagger();
Expand Down
57 changes: 5 additions & 52 deletions ExceptionAll/Helpers/ServiceCollectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,23 @@ public static class ServiceCollectionHelper
/// <returns></returns>
public static IServiceCollection AddExceptionAll<T>(this IServiceCollection services) where T : class, IExceptionAllConfiguration
{
services.AddSingleton<IErrorResponseService, ErrorResponseService>();
services.AddSingleton<IActionResultService, ActionResultService>();
services.AddSingleton<IContextConfigurationService, ContextConfigurationService>();

services.Scan(
x => x.FromAssemblyOf<T>()
.AddClasses(c => c.AssignableTo<IExceptionAllConfiguration>())
.AsImplementedInterfaces()
.WithSingletonLifetime());

var serviceProvider = services.BuildServiceProvider();
var actionResultService = serviceProvider.GetRequiredService<IActionResultService>();
var contextConfiguration = serviceProvider.GetRequiredService<IContextConfigurationService>();
var configuration = serviceProvider.GetRequiredService<IExceptionAllConfiguration>();
//var errorResponseService = serviceProvider.GetRequiredService<IErrorResponseService>();

//errorResponseService?.AddErrorResponses(configuration.ErrorResponses.ToList());

if (configuration.ContextConfiguration is not null)
contextConfiguration.Configure(configuration.ContextConfiguration);
services.AddSingleton<IContextConfigurationService, ContextConfigurationService>();
services.AddSingleton<IErrorResponseService, ErrorResponseService>();

// Adds an exception filter to
services.AddMvc(options => { options.Filters.Add<ExceptionFilter>(); });

// Removes the default response from being returned on validation error
services.Configure<ApiBehaviorOptions>(
options =>
services.AddOptions<ApiBehaviorOptions>()
.Configure<IActionResultService>((options, ars) =>
{
options.InvalidModelStateResponseFactory = context =>
{
Expand All @@ -45,7 +35,7 @@ public static IServiceCollection AddExceptionAll<T>(this IServiceCollection serv
errors.AddRange(value.Errors.Select(error => new ErrorDetail(key, error.ErrorMessage)));
}

return actionResultService.GetResponse<BadRequestDetails>(
return ars.GetResponse<BadRequestDetails>(
context,
"Invalid request model",
errors.Any() ? errors : null);
Expand Down Expand Up @@ -79,41 +69,4 @@ public static IServiceCollection WithExceptionAllSwaggerExamples(this IServiceCo
{
return services.AddSwaggerExamplesFromAssemblyOf<BadRequestDetailsExample>();
}

/// <summary>
/// Applies the configuration provided by the user to the error response container
/// </summary>
/// <param name="services"></param>
/// <exception cref="ArgumentNullException"></exception>
public static void AddExceptionAll(this IServiceProvider services)
{
var contextConfiguration = services.GetService<IContextConfigurationService>();
var errorResponseService = services.GetService<IErrorResponseService>();
var configuration = services.GetService<IExceptionAllConfiguration>();

if (configuration is null || contextConfiguration is null) return;

errorResponseService?.AddErrorResponses(configuration.ErrorResponses.ToList());

if (configuration.ContextConfiguration is not null)
contextConfiguration.Configure(configuration.ContextConfiguration);
}

/// <summary>
/// Add all error responses into the response collection
/// </summary>
/// <param name="service"></param>
/// <param name="errorResponses"></param>
private static void AddErrorResponses(
this IErrorResponseService service,
List<IErrorResponse> errorResponses)
{
if (errorResponses == null || !errorResponses.Any())
throw new ArgumentNullException(nameof(errorResponses));

foreach (var errorResponse in errorResponses)
{
service.AddErrorResponse(errorResponse);
}
}
}
6 changes: 0 additions & 6 deletions ExceptionAll/Interfaces/IContextConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ namespace ExceptionAll.Interfaces;

public interface IContextConfigurationService
{
/// <summary>
/// Allows the user to configure additional properties shown in the response which can be derived from the HttpContext
/// </summary>
/// <param name="configuration"></param>
void Configure(Dictionary<string, Func<HttpContext, object>> configuration);

/// <summary>
/// Reads data from the HttpContext and maps it according to the configuration provided
/// </summary>
Expand Down
7 changes: 4 additions & 3 deletions ExceptionAll/Services/ContextConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ namespace ExceptionAll.Services;

public class ContextConfigurationService : IContextConfigurationService
{
private Dictionary<string, Func<HttpContext, object>>? _contextMappings;
private readonly Dictionary<string, Func<HttpContext, object>>? _contextMappings;

public void Configure(Dictionary<string, Func<HttpContext, object>> configuration)
public ContextConfigurationService(IExceptionAllConfiguration configuration)
{
_contextMappings = new Dictionary<string, Func<HttpContext, object>>(configuration);
if (configuration.ContextConfiguration is not null)
_contextMappings = new(configuration.ContextConfiguration);
}

public IReadOnlyDictionary<string, object>? GetContextDetails(HttpContext context, List<ErrorDetail>? errors = null)
Expand Down
13 changes: 12 additions & 1 deletion ExceptionAll/Services/ErrorResponseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ public class ErrorResponseService : IErrorResponseService
private readonly ILogger<IErrorResponseService> _logger;
private Dictionary<Type, IErrorResponse> ErrorResponses { get; } = new();

public ErrorResponseService(ILogger<IErrorResponseService> logger)
public ErrorResponseService(ILogger<IErrorResponseService> logger, IExceptionAllConfiguration configuration)
{
_logger = logger;

AddErrorResponses(configuration.ErrorResponses);
}

private void AddErrorResponses(List<IErrorResponse> errorResponses)
{
if (errorResponses == null || !errorResponses.Any())
throw new ArgumentNullException(nameof(errorResponses));

foreach (var errorResponse in errorResponses)
AddErrorResponse(errorResponse);
}

public void AddErrorResponse(IErrorResponse response)
Expand Down

0 comments on commit 412a510

Please sign in to comment.