namespace CQRSAndMediatRDemo.Models
{
public class StudentDetails
{
public int Id { get; set; }
public string StudentName { get; set; }
public string StudentEmail { get; set; }
public string StudentAddress { get; set; }
public int StudentAge { get; set; }
}
}
步骤 7
接下来,在 Data 文件夹中添加 DbContextClass
using CQRSAndMediatRDemo.Models;
using Microsoft.EntityFrameworkCore;
namespace CQRSAndMediatRDemo.Data
{
public class DbContextClass : DbContext
{
protected readonly IConfiguration Configuration;
public DbContextClass(IConfiguration configuration)
{
Configuration = configuration;
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
}
public DbSet<StudentDetails> Students { get; set; }
}
}
步骤 8
创建一个学生资料库和一个与之相关的课程。
IStudentRepository
using CQRSAndMediatRDemo.Models;
namespace CQRSAndMediatRDemo.Repositories
{
public interface IStudentRepository
{
public Task<List<StudentDetails>> GetStudentListAsync();
public Task<StudentDetails> GetStudentByIdAsync(int Id);
public Task<StudentDetails> AddStudentAsync(StudentDetails studentDetails);
public Task<int> UpdateStudentAsync(StudentDetails studentDetails);
public Task<int> DeleteStudentAsync(int Id);
}
}
StudentRepository
using CQRSAndMediatRDemo.Data;
using CQRSAndMediatRDemo.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Numerics;
namespace CQRSAndMediatRDemo.Repositories
{
public class StudentRepository : IStudentRepository
{
private readonly DbContextClass _dbContext;
public StudentRepository(DbContextClass dbContext)
{
_dbContext = dbContext;
}
public async Task<StudentDetails> AddStudentAsync(StudentDetails studentDetails)
{
var result = _dbContext.Students.Add(studentDetails);
await _dbContext.SaveChangesAsync();
return result.Entity;
}
public async Task<int> DeleteStudentAsync(int Id)
{
var filteredData = _dbContext.Students.Where(x => x.Id == Id).FirstOrDefault();
_dbContext.Students.Remove(filteredData);
return await _dbContext.SaveChangesAsync();
}
public async Task<StudentDetails> GetStudentByIdAsync(int Id)
{
return await _dbContext.Students.Where(x => x.Id == Id).FirstOrDefaultAsync();
}
public async Task<List<StudentDetails>> GetStudentListAsync()
{
return await _dbContext.Students.ToListAsync();
}
public async Task<int> UpdateStudentAsync(StudentDetails studentDetails)
{
_dbContext.Students.Update(studentDetails);
return await _dbContext.SaveChangesAsync();
}
}
}
步骤 9
之后,添加读取查询
GetStudentListQuery
using CQRSAndMediatRDemo.Models;
using MediatR;
namespace CQRSAndMediatRDemo.Queries
{
public class GetStudentListQuery : IRequest<List<StudentDetails>>
{
}
}
GetStudentByIdQuery
using CQRSAndMediatRDemo.Models;
using MediatR;
namespace CQRSAndMediatRDemo.Queries
{
public class GetStudentByIdQuery : IRequest<StudentDetails>
{
public int Id { get; set; }
}
}
步骤 10
接下来,创建不同的命令
CreateStudentCommand
using CQRSAndMediatRDemo.Models;
using MediatR;
namespace CQRSAndMediatRDemo.Commands
{
public class CreateStudentCommand : IRequest<StudentDetails>
{
public string StudentName { get; set; }
public string StudentEmail { get; set; }
public string StudentAddress { get; set; }
public int StudentAge { get; set; }
public CreateStudentCommand(string studentName, string studentEmail, string studentAddress, int studentAge)
{
StudentName = studentName;
StudentEmail = studentEmail;
StudentAddress = studentAddress;
StudentAge = studentAge;
}
}
}
UpdateStudentCommand
using MediatR;
namespace CQRSAndMediatRDemo.Commands
{
public class UpdateStudentCommand : IRequest<int>
{
public int Id { get; set; }
public string StudentName { get; set; }
public string StudentEmail { get; set; }
public string StudentAddress { get; set; }
public int StudentAge { get; set; }
public UpdateStudentCommand(int id, string studentName, string studentEmail, string studentAddress, int studentAge)
{
Id = id;
StudentName = studentName;
StudentEmail = studentEmail;
StudentAddress = studentAddress;
StudentAge = studentAge;
}
}
}
DeleteStudentCommand
using MediatR;
namespace CQRSAndMediatRDemo.Commands
{
public class DeleteStudentCommand : IRequest<int>
{
public int Id { get; set; }
}
}
步骤 11
现在,添加查询和命令处理程序
GetStudentListHandler
using CQRSAndMediatRDemo.Models;
using CQRSAndMediatRDemo.Queries;
using CQRSAndMediatRDemo.Repositories;
using MediatR;
using System.Numerics;
namespace CQRSAndMediatRDemo.Handlers
{
public class GetStudentListHandler : IRequestHandler<GetStudentListQuery, List<StudentDetails>>
{
private readonly IStudentRepository _studentRepository;
public GetStudentListHandler(IStudentRepository studentRepository)
{
_studentRepository = studentRepository;
}
public async Task<List<StudentDetails>> Handle(GetStudentListQuery query, CancellationToken cancellationToken)
{
return await _studentRepository.GetStudentListAsync();
}
}
}
GetStudentByIdHandler
using CQRSAndMediatRDemo.Models;
using CQRSAndMediatRDemo.Queries;
using CQRSAndMediatRDemo.Repositories;
using MediatR;
using System.Numerics;
namespace CQRSAndMediatRDemo.Handlers
{
public class GetStudentByIdHandler : IRequestHandler<GetStudentByIdQuery, StudentDetails>
{
private readonly IStudentRepository _studentRepository;
public GetStudentByIdHandler(IStudentRepository studentRepository)
{
_studentRepository = studentRepository;
}
public async Task<StudentDetails> Handle(GetStudentByIdQuery query, CancellationToken cancellationToken)
{
return await _studentRepository.GetStudentByIdAsync(query.Id);
}
}
}
CreateStudentHandler
using CQRSAndMediatRDemo.Commands;
using CQRSAndMediatRDemo.Models;
using CQRSAndMediatRDemo.Repositories;
using MediatR;
namespace CQRSAndMediatRDemo.Handlers
{
public class CreateStudentHandler: IRequestHandler<CreateStudentCommand, StudentDetails>
{
private readonly IStudentRepository _studentRepository;
public CreateStudentHandler(IStudentRepository studentRepository)
{
_studentRepository = studentRepository;
}
public async Task<StudentDetails> Handle(CreateStudentCommand command, CancellationToken cancellationToken)
{
var studentDetails = new StudentDetails()
{
StudentName = command.StudentName,
StudentEmail = command.StudentEmail,
StudentAddress = command.StudentAddress,
StudentAge = command.StudentAge
};
return await _studentRepository.AddStudentAsync(studentDetails);
}
}
}
UpdateStudentHandler
using CQRSAndMediatRDemo.Commands;
using CQRSAndMediatRDemo.Repositories;
using MediatR;
namespace CQRSAndMediatRDemo.Handlers
{
public class UpdateStudentHandler : IRequestHandler<UpdateStudentCommand, int>
{
private readonly IStudentRepository _studentRepository;
public UpdateStudentHandler(IStudentRepository studentRepository)
{
_studentRepository = studentRepository;
}
public async Task<int> Handle(UpdateStudentCommand command, CancellationToken cancellationToken)
{
var studentDetails = await _studentRepository.GetStudentByIdAsync(command.Id);
if (studentDetails == null)
return default;
studentDetails.StudentName = command.StudentName;
studentDetails.StudentEmail = command.StudentEmail;
studentDetails.StudentAddress = command.StudentAddress;
studentDetails.StudentAge = command.StudentAge;
return await _studentRepository.UpdateStudentAsync(studentDetails);
}
}
}
DeleteStudentHandler
using CQRSAndMediatRDemo.Commands;
using CQRSAndMediatRDemo.Repositories;
using MediatR;
namespace CQRSAndMediatRDemo.Handlers
{
public class DeleteStudentHandler : IRequestHandler<DeleteStudentCommand, int>
{
private readonly IStudentRepository _studentRepository;
public DeleteStudentHandler(IStudentRepository studentRepository)
{
_studentRepository = studentRepository;
}
public async Task<int> Handle(DeleteStudentCommand command, CancellationToken cancellationToken)
{
var studentDetails = await _studentRepository.GetStudentByIdAsync(command.Id);
if (studentDetails == null)
return default;
return await _studentRepository.DeleteStudentAsync(studentDetails.Id);
}
}
}
using CQRSAndMediatRDemo.Data;
using CQRSAndMediatRDemo.Repositories;
using MediatR;
using Microsoft.AspNetCore.Hosting;
using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddMediatR(Assembly.GetExecutingAssembly());
builder.Services.AddDbContext<DbContextClass>();
builder.Services.AddScoped<IStudentRepository, StudentRepository>();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
步骤 14
接下来执行数据库迁移和更新命令
add-migration “initial”
update-database
步骤 15
之后,创建 Students Controller 并在其中注入 MediatR 服务以发送查询和命令
using CQRSAndMediatRDemo.Commands;
using CQRSAndMediatRDemo.Models;
using CQRSAndMediatRDemo.Queries;
using CQRSAndMediatRDemo.Repositories;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
namespace CQRSAndMediatRDemo.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StudentsController : ControllerBase
{
private readonly IMediator mediator;
public StudentsController(IMediator mediator)
{
this.mediator = mediator;
}
[HttpGet]
public async Task<List<StudentDetails>> GetStudentListAsync()
{
var studentDetails = await mediator.Send(new GetStudentListQuery());
return studentDetails;
}
[HttpGet("studentId")]
public async Task<StudentDetails> GetStudentByIdAsync(int studentId)
{
var studentDetails = await mediator.Send(new GetStudentByIdQuery() { Id = studentId });
return studentDetails;
}
[HttpPost]
public async Task<StudentDetails> AddStudentAsync(StudentDetails studentDetails)
{
var studentDetail = await mediator.Send(new CreateStudentCommand(
studentDetails.StudentName,
studentDetails.StudentEmail,
studentDetails.StudentAddress,
studentDetails.StudentAge));
return studentDetail;
}
[HttpPut]
public async Task<int> UpdateStudentAsync(StudentDetails studentDetails)
{
var isStudentDetailUpdated = await mediator.Send(new UpdateStudentCommand(
studentDetails.Id,
studentDetails.StudentName,
studentDetails.StudentEmail,
studentDetails.StudentAddress,
studentDetails.StudentAge));
return isStudentDetailUpdated;
}
[HttpDelete]
public async Task<int> DeleteStudentAsync(int Id)
{
return await mediator.Send(new DeleteStudentCommand() { Id = Id });
}
}
}