Files
hrynco-notification-service/HrynCo.NotificationService.Services/Behaviors/TransactionBehavior.cs
T
Anatolii Grynchuk 0d1aa4f6be chore: update package versions and refactor TransactionBehavior
- Upgrade EF Core packages to 10.0.x and HrynCo.DAL.Abstract to 1.0.10
- Refactor TransactionBehavior to simplify transaction handling logic
2026-05-12 21:37:38 +03:00

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);
}