using HrynCo.Common; using HrynCo.NotificationService.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( () => _unitOfWork.ExecuteInTransactionAsync(async () => { TResponse response = await next(); await _unitOfWork.SaveChangesAsync(cancellationToken); return response; }), typeof(TRequest).Name); }