-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUsageAwareRequestHandlerDecorator.cs
More file actions
28 lines (25 loc) · 1.16 KB
/
UsageAwareRequestHandlerDecorator.cs
File metadata and controls
28 lines (25 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
namespace softaware.Cqs.Decorators.UsageAware;
/// <summary>
/// A decorator for tracking command executions with UsageAware.
/// </summary>
/// <typeparam name="TRequest">The request to execute.</typeparam>
/// <typeparam name="TResult">The type of the result.</typeparam>
public class UsageAwareRequestHandlerDecorator<TRequest, TResult> : IRequestHandler<TRequest, TResult>
where TRequest : IRequest<TResult>
{
private readonly UsageAwareLogger<TRequest, TResult> logger;
private readonly IRequestHandler<TRequest, TResult> decoratee;
public UsageAwareRequestHandlerDecorator(
UsageAwareLogger<TRequest, TResult> logger,
IRequestHandler<TRequest, TResult> decoratee)
{
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
this.decoratee = decoratee ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<TResult> HandleAsync(TRequest command, CancellationToken cancellationToken)
{
var result = default(TResult);
await this.logger.TimeAndLogAsync(async () => result = await this.decoratee.HandleAsync(command, cancellationToken));
return result!;
}
}