using HrynCo.Common; using HrynCo.DAL.Abstract; using MediatR; namespace HrynCo.NotificationService.Services.Behaviors; public class TransactionBehavior : IPipelineBehavior where TRequest : notnull { private readonly IUnitOfWork _unitOfWork; private readonly IProfiler _profiler; public TransactionBehavior(IUnitOfWork unitOfWork, IProfiler profiler) { _unitOfWork = unitOfWork; _profiler = profiler; } public Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) => _profiler.MeasureExecutionAsync( async () => { TResponse? response = default; await _unitOfWork.ExecuteInTransactionAsync(async () => { response = await next(); }); return response!; }, typeof(TRequest).Name); }