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.Reflection;
using AutoMapper;
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Bson;
@@ -18,18 +17,17 @@ namespace Shop.Query;
[ExcludeFromCodeCoverage]
public static class ConfigureServices
{
private static readonly Assembly QueryAssembly = Assembly.GetAssembly(typeof(IQueryMarker));
/// <summary>
/// Adds query handlers to the service collection.
/// </summary>
/// <param name="services">The service collection.</param>
public static IServiceCollection AddQueryHandlers(this IServiceCollection services)
{
var assembly = Assembly.GetAssembly(typeof(IQueryMarker));
return services
.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(assembly))
.AddSingleton<IMapper>(new Mapper(new MapperConfiguration(cfg => cfg.AddMaps(assembly))))
.AddValidatorsFromAssembly(assembly);
}
public static IServiceCollection AddQueryHandlers(this IServiceCollection services) =>
services
.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(QueryAssembly))
.AddAutoMapper(cfg => cfg.AddMaps(QueryAssembly))
.AddValidatorsFromAssembly(QueryAssembly);
/// <summary>
/// Adds the read database context to the service collection.