-
Notifications
You must be signed in to change notification settings - Fork 672
Expand file tree
/
Copy pathRegisterCustomerCommandHandler.cs
More file actions
47 lines (40 loc) · 1.8 KB
/
RegisterCustomerCommandHandler.cs
File metadata and controls
47 lines (40 loc) · 1.8 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using SampleProject.Application.Configuration.Commands;
using SampleProject.Domain.Customers;
using SampleProject.Domain.Customers.Orders;
using SampleProject.Domain.SeedWork;
namespace SampleProject.Application.Customers.RegisterCustomer
{
public class RegisterCustomerCommandHandler : ICommandHandler<RegisterCustomerCommand, CustomerDto>
{
private readonly ICustomerRepository _customerRepository;
private readonly ICustomerUniquenessChecker _customerUniquenessChecker;
private readonly IUnitOfWork _unitOfWork;
private readonly ICustomerEmailChecker _customerEmailChecker;
public RegisterCustomerCommandHandler(
ICustomerRepository customerRepository,
ICustomerUniquenessChecker customerUniquenessChecker,
ICustomerEmailChecker customerEmailChecker,
IUnitOfWork unitOfWork)
{
this._customerRepository = customerRepository;
_customerUniquenessChecker = customerUniquenessChecker;
_customerEmailChecker = customerEmailChecker;
_unitOfWork = unitOfWork;
}
public async Task<CustomerDto> Handle(RegisterCustomerCommand request, CancellationToken cancellationToken)
{
var customer = Customer.CreateRegistered(request.Email, request.Name, this._customerUniquenessChecker, this._customerEmailChecker);
if (customer == null)
{
// What is the best way to return error to user?
return new CustomerDto();
}
await this._customerRepository.AddAsync(customer);
await this._unitOfWork.CommitAsync(cancellationToken);
return new CustomerDto { Id = customer.Id.Value };
}
}
}