Skip to content

Instantly share code, notes, and snippets.

@lakshithacodes
Last active May 30, 2021 06:17
Show Gist options
  • Save lakshithacodes/6d7a089b418c72b81594415cdb038ab7 to your computer and use it in GitHub Desktop.
Save lakshithacodes/6d7a089b418c72b81594415cdb038ab7 to your computer and use it in GitHub Desktop.
SaveCustomerCommandHandler - CQRS with MediatR
public class SaveCustomerCommandHandler : IRequestHandler<SaveCustomerRequestModel, Unit>
{
private readonly CustomerDbContext context;
public SaveCustomerCommandHandler(CustomerDbContext context)
{
this.context = context;
}
public async Task<Unit> Handle(SaveCustomerRequestModel saveCustomerRequestModel,
CancellationToken cancellationToken)
{
var newCustomer = new PersonalDetails
{
Name = saveCustomerRequestModel.Name,
Title = saveCustomerRequestModel.Title,
City = saveCustomerRequestModel.City,
LoyaltyPoints = saveCustomerRequestModel.LoyaltyPoints
};
this.context.PersonalDetails.Add(newCustomer);
await this.context.SaveChangesAsync();
return Unit.Value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment