Cool UnitOfWork convenience method (:

This commit is contained in:
lumijiez
2025-06-21 21:15:10 +03:00
parent ec78744d19
commit d9dfafe07a
4 changed files with 52 additions and 35 deletions

View File

@@ -16,32 +16,22 @@ public class CreateProductCommand : IRequest<ProductDto>
public Guid? CategoryId { get; set; }
}
public class CreateProductHandler(IUnitOfWork unitOfWork, IMapper mapper) : IRequestHandler<CreateProductCommand, ProductDto>
public class CreateProductHandler(IUnitOfWork uw, IMapper mapper) : IRequestHandler<CreateProductCommand, ProductDto>
{
public async Task<ProductDto> Handle(CreateProductCommand request, CancellationToken cancellationToken)
{
await unitOfWork.BeginTransactionAsync(cancellationToken);
try
return await uw.TransactAsync(async () =>
{
var product = mapper.Map<Product>(request);
var createdProduct = await uw.ProductRepository.AddAsync(product, cancellationToken);
var createdProduct = await unitOfWork.ProductRepository.AddAsync(product, cancellationToken);
if (createdProduct.CategoryId.HasValue)
{
createdProduct.Category = (await unitOfWork.CategoryRepository.GetByIdAsync(createdProduct.CategoryId.Value, cancellationToken))!;
createdProduct.Category =
(await uw.CategoryRepository.GetByIdAsync(createdProduct.CategoryId.Value, cancellationToken))!;
}
await unitOfWork.SaveAsync(cancellationToken);
await unitOfWork.CommitTransactionAsync(cancellationToken);
return mapper.Map<ProductDto>(createdProduct);
}
catch
{
await unitOfWork.RollbackTransactionAsync(cancellationToken);
throw;
}
}, cancellationToken);
}
}