nuget包
- Autofac
- Autofac.Extensions.DependencyInjection
- MediatR
- MediatR.Extensions.Microsoft.DependencyInjection
一、Command
public class CreateStudentCommand : IRequest<int>
{
public int ID {
get; set; }
public string Name {
get; set; }
public string Sex {
get; set; }
public string Phone {
get; set; }
public string Address {
get; set; }
}
二、CommandHandler
public class CreateStudentCommandHandler : IRequestHandler<CreateStudentCommand, int>
{
private readonly StudentDbContext _studentDbContext;
private readonly IMediator _mediator;
public CreateStudentCommandHandler(StudentDbContext studentDbContext , IMediator mediator)
{
_studentDbContext = studentDbContext;
_mediator = mediator;
}
public async Task<int> Handle(CreateStudentCommand request, CancellationToken cancellationToken)
{
Student student = new Student
{
Name = "123"