Refactor Catalogs to use MediatR and CQRS pattern
Replaced direct service usage in CatalogsController with MediatR-based commands and queries for all CRUD operations. Added command/query and handler classes for Catalog operations. Updated dependency injection to register MediatR and removed ICatalogService. Improved code maintainability and testability by adopting CQRS architecture.
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
using DbFirst.Application.Catalogs;
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application.Catalogs.Commands;
|
||||
|
||||
public record CreateCatalogCommand(CatalogWriteDto Dto) : IRequest<CatalogReadDto?>;
|
||||
@@ -0,0 +1,36 @@
|
||||
using AutoMapper;
|
||||
using DbFirst.Domain.Entities;
|
||||
using DbFirst.Domain.Repositories;
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application.Catalogs.Commands;
|
||||
|
||||
public class CreateCatalogHandler : IRequestHandler<CreateCatalogCommand, CatalogReadDto?>
|
||||
{
|
||||
private readonly ICatalogRepository _repository;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public CreateCatalogHandler(ICatalogRepository repository, IMapper mapper)
|
||||
{
|
||||
_repository = repository;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<CatalogReadDto?> Handle(CreateCatalogCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var existing = await _repository.GetByTitleAsync(request.Dto.CatTitle, cancellationToken);
|
||||
if (existing != null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var entity = _mapper.Map<VwmyCatalog>(request.Dto);
|
||||
entity.AddedWho = "system";
|
||||
entity.AddedWhen = DateTime.UtcNow;
|
||||
entity.ChangedWho = "system";
|
||||
entity.ChangedWhen = DateTime.UtcNow;
|
||||
|
||||
var created = await _repository.InsertAsync(entity, cancellationToken);
|
||||
return _mapper.Map<CatalogReadDto>(created);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application.Catalogs.Commands;
|
||||
|
||||
public record DeleteCatalogCommand(int Id) : IRequest<bool>;
|
||||
@@ -0,0 +1,19 @@
|
||||
using DbFirst.Domain.Repositories;
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application.Catalogs.Commands;
|
||||
|
||||
public class DeleteCatalogHandler : IRequestHandler<DeleteCatalogCommand, bool>
|
||||
{
|
||||
private readonly ICatalogRepository _repository;
|
||||
|
||||
public DeleteCatalogHandler(ICatalogRepository repository)
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
public async Task<bool> Handle(DeleteCatalogCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _repository.DeleteAsync(request.Id, cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
using DbFirst.Application.Catalogs;
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application.Catalogs.Commands;
|
||||
|
||||
public record UpdateCatalogCommand(int Id, CatalogWriteDto Dto) : IRequest<CatalogReadDto?>;
|
||||
@@ -0,0 +1,38 @@
|
||||
using AutoMapper;
|
||||
using DbFirst.Domain.Entities;
|
||||
using DbFirst.Domain.Repositories;
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application.Catalogs.Commands;
|
||||
|
||||
public class UpdateCatalogHandler : IRequestHandler<UpdateCatalogCommand, CatalogReadDto?>
|
||||
{
|
||||
private readonly ICatalogRepository _repository;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public UpdateCatalogHandler(ICatalogRepository repository, IMapper mapper)
|
||||
{
|
||||
_repository = repository;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<CatalogReadDto?> Handle(UpdateCatalogCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var existing = await _repository.GetByIdAsync(request.Id, cancellationToken);
|
||||
if (existing == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var entity = _mapper.Map<VwmyCatalog>(request.Dto);
|
||||
entity.Guid = request.Id;
|
||||
entity.CatTitle = existing.CatTitle;
|
||||
entity.AddedWho = existing.AddedWho;
|
||||
entity.AddedWhen = existing.AddedWhen;
|
||||
entity.ChangedWho = "system";
|
||||
entity.ChangedWhen = DateTime.UtcNow;
|
||||
|
||||
var updated = await _repository.UpdateAsync(request.Id, entity, cancellationToken);
|
||||
return updated == null ? null : _mapper.Map<CatalogReadDto>(updated);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using AutoMapper;
|
||||
using DbFirst.Domain.Repositories;
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application.Catalogs.Queries;
|
||||
|
||||
public class GetAllCatalogsHandler : IRequestHandler<GetAllCatalogsQuery, List<CatalogReadDto>>
|
||||
{
|
||||
private readonly ICatalogRepository _repository;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public GetAllCatalogsHandler(ICatalogRepository repository, IMapper mapper)
|
||||
{
|
||||
_repository = repository;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<List<CatalogReadDto>> Handle(GetAllCatalogsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var items = await _repository.GetAllAsync(cancellationToken);
|
||||
return _mapper.Map<List<CatalogReadDto>>(items);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
using DbFirst.Application.Catalogs;
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application.Catalogs.Queries;
|
||||
|
||||
public record GetAllCatalogsQuery : IRequest<List<CatalogReadDto>>;
|
||||
@@ -0,0 +1,23 @@
|
||||
using AutoMapper;
|
||||
using DbFirst.Domain.Repositories;
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application.Catalogs.Queries;
|
||||
|
||||
public class GetCatalogByIdHandler : IRequestHandler<GetCatalogByIdQuery, CatalogReadDto?>
|
||||
{
|
||||
private readonly ICatalogRepository _repository;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public GetCatalogByIdHandler(ICatalogRepository repository, IMapper mapper)
|
||||
{
|
||||
_repository = repository;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<CatalogReadDto?> Handle(GetCatalogByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var item = await _repository.GetByIdAsync(request.Id, cancellationToken);
|
||||
return item == null ? null : _mapper.Map<CatalogReadDto>(item);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
using DbFirst.Application.Catalogs;
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application.Catalogs.Queries;
|
||||
|
||||
public record GetCatalogByIdQuery(int Id) : IRequest<CatalogReadDto?>;
|
||||
@@ -10,6 +10,7 @@
|
||||
<PackageReference Include="AutoMapper" Version="12.0.1" />
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
|
||||
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="11.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MediatR;
|
||||
|
||||
namespace DbFirst.Application;
|
||||
|
||||
@@ -7,6 +8,7 @@ public static class DependencyInjection
|
||||
public static IServiceCollection AddApplication(this IServiceCollection services)
|
||||
{
|
||||
services.AddAutoMapper(typeof(DependencyInjection).Assembly);
|
||||
services.AddMediatR(typeof(DependencyInjection).Assembly);
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user