-
Notifications
You must be signed in to change notification settings - Fork 649
/
UnitOfWorkCommandHandlerWithResultDecorator.cs
49 lines (40 loc) · 1.63 KB
/
UnitOfWorkCommandHandlerWithResultDecorator.cs
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SampleProject.Application;
using SampleProject.Application.Configuration.Commands;
using SampleProject.Domain.SeedWork;
using SampleProject.Infrastructure.Database;
namespace SampleProject.Infrastructure.Processing
{
public class UnitOfWorkCommandHandlerWithResultDecorator<T, TResult> : ICommandHandler<T, TResult> where T : ICommand<TResult>
{
private readonly ICommandHandler<T, TResult> _decorated;
private readonly IUnitOfWork _unitOfWork;
private readonly OrdersContext _ordersContext;
public UnitOfWorkCommandHandlerWithResultDecorator(
ICommandHandler<T, TResult> decorated,
IUnitOfWork unitOfWork,
OrdersContext ordersContext)
{
_decorated = decorated;
_unitOfWork = unitOfWork;
_ordersContext = ordersContext;
}
public async Task<TResult> Handle(T command, CancellationToken cancellationToken)
{
var result = await this._decorated.Handle(command, cancellationToken);
if (command is InternalCommandBase<TResult>)
{
var internalCommand = await _ordersContext.InternalCommands.FirstOrDefaultAsync(x => x.Id == command.Id, cancellationToken: cancellationToken);
if (internalCommand != null)
{
internalCommand.ProcessedDate = DateTime.UtcNow;
}
}
await this._unitOfWork.CommitAsync(cancellationToken);
return result;
}
}
}