refactor(ConfigureServices): simplify AddQueryHandlers method and improve AutoMapper integration
This commit is contained in:
@@ -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.
|
||||||
|
|||||||
Reference in New Issue
Block a user