0d1aa4f6be
- Upgrade EF Core packages to 10.0.x and HrynCo.DAL.Abstract to 1.0.10 - Refactor TransactionBehavior to simplify transaction handling logic
33 lines
990 B
C#
33 lines
990 B
C#
using HrynCo.Common;
|
|
using HrynCo.DAL.Abstract;
|
|
using MediatR;
|
|
|
|
namespace HrynCo.NotificationService.Services.Behaviors;
|
|
|
|
public class TransactionBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
|
|
where TRequest : notnull
|
|
{
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
private readonly IProfiler _profiler;
|
|
|
|
public TransactionBehavior(IUnitOfWork unitOfWork, IProfiler profiler)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
_profiler = profiler;
|
|
}
|
|
|
|
public Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken) =>
|
|
_profiler.MeasureExecutionAsync(
|
|
async () =>
|
|
{
|
|
TResponse? response = default;
|
|
await _unitOfWork.ExecuteInTransactionAsync(async () =>
|
|
{
|
|
response = await next();
|
|
});
|
|
|
|
return response!;
|
|
},
|
|
typeof(TRequest).Name);
|
|
}
|