Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save wellingtonjhn/3be2dd6b2b4a6ffade6180a36ec762fd to your computer and use it in GitHub Desktop.

Select an option

Save wellingtonjhn/3be2dd6b2b4a6ffade6180a36ec762fd to your computer and use it in GitHub Desktop.
Handler with NotificationContext
public class CreateCustomerHandler : IRequestHandler<CreateCustomer, Guid>
{
private readonly NotificationContext _notificationContext;
private readonly ICustomerRepository _customerRepository;
public CreateCustomerHandler(
NotificationContext notificationContext,
ICustomerRepository customerRepository)
{
_notificationContext = notificationContext;
_customerRepository = customerRepository;
}
public async Task<Guid> Handle(CreateCustomer request, CancellationToken cancellationToken)
{
var customer = new Customer(request.Name, request.Email);
if (customer.Invalid)
{
_notificationContext.AddNotifications(customer.ValidationResult);
return Guid.Empty;
}
await _customerRepository.Save(customer);
return customer.Id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment