92be035f51
- Add HrynCo.Common to Services project - TransactionBehavior now uses IProfiler.MeasureExecutionAsync: MeasureExecutionAsync -> ExecuteInTransactionAsync -> next() -> SaveChangesAsync - Profiler logs Start/End with duration + memory delta via Serilog PerformanceLog context - Register IProfiler as singleton in ServiceCollectionExtensions (uses Log.Logger) Ref: IT-628 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
28 lines
972 B
C#
28 lines
972 B
C#
using HrynCo.Common;
|
|
using HrynCo.NotificationService.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(
|
|
() => _unitOfWork.ExecuteInTransactionAsync(async () =>
|
|
{
|
|
TResponse response = await next();
|
|
await _unitOfWork.SaveChangesAsync(cancellationToken);
|
|
return response;
|
|
}),
|
|
typeof(TRequest).Name);
|
|
} |