refactor(ConfigureServices): simplify AddQueryHandlers method and improve AutoMapper integration

This commit is contained in:
Jean Gatto
2025-08-21 11:18:21 -03:00
parent 6565dd44ea
commit 1cb4c92fdd
+7 -9
View File
@@ -1,6 +1,5 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Reflection; using System.Reflection;
using AutoMapper;
using FluentValidation; using FluentValidation;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using MongoDB.Bson; using MongoDB.Bson;
@@ -18,18 +17,17 @@ namespace Shop.Query;
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
public static class ConfigureServices public static class ConfigureServices
{ {
private static readonly Assembly QueryAssembly = Assembly.GetAssembly(typeof(IQueryMarker));
/// <summary> /// <summary>
/// Adds query handlers to the service collection. /// Adds query handlers to the service collection.
/// </summary> /// </summary>
/// <param name="services">The service collection.</param> /// <param name="services">The service collection.</param>
public static IServiceCollection AddQueryHandlers(this IServiceCollection services) public static IServiceCollection AddQueryHandlers(this IServiceCollection services) =>
{ services
var assembly = Assembly.GetAssembly(typeof(IQueryMarker)); .AddMediatR(cfg => cfg.RegisterServicesFromAssembly(QueryAssembly))
return services .AddAutoMapper(cfg => cfg.AddMaps(QueryAssembly))
.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(assembly)) .AddValidatorsFromAssembly(QueryAssembly);
.AddSingleton<IMapper>(new Mapper(new MapperConfiguration(cfg => cfg.AddMaps(assembly))))
.AddValidatorsFromAssembly(assembly);
}
/// <summary> /// <summary>
/// Adds the read database context to the service collection. /// Adds the read database context to the service collection.