推荐一个关于 《ASP.NET Core 整洁架构实践》 的优质学习视频。
课程视频地址如下:
ASP.NET Core 整洁架构实践(中文语音版) :
https://www.bilibili.com/video/BV1D5yJBSErH
ASP.NET Core Clean Architecture(英文原版) :
https://www.bilibili.com/video/BV1u3CrBuEdk

另一个与之高度关联的课程 《C# 中的整洁代码原则》 也值得一看:
https://www.bilibili.com/video/BV1vHkfBhEGs

在实践整洁架构时,虽然没有找到讲师 Felipe Gavilan 的原始项目源码,但网上有许多学习该课程后创建的、思路一致的开源项目可供参考。
例如这个项目:
https://github.com/makhirmjd/CleanTeeth
这个课程包含了许多值得深入学习的知识点。依赖倒置原则 作为核心思想贯穿了整个实践过程。其中,最让人耳目一新的是对 Mediator(中介者)模式 的应用。它作为方法与具体实现之间的桥梁,极大地提升了代码的灵活性。当然,这种模式也可能在一定程度上增加代码的理解和调试复杂度。
下面通过一段核心代码来感受其实现方式:
//申明Mediator接口
public interface IMediator
{
Task<TResponse> Send<TResponse>(IRequest<TResponse> request);
Task Send(IRequest request);
}
//定义Mediator方法
public class SimpleMediator(IServiceProvider serviceProvider) : IMediator
{
public async Task<TResponse> Send<TResponse>(IRequest<TResponse> request)
{
await ApplyValidations(request);
Type handlerType = typeof(IRequestHandler<,>).MakeGenericType(request.GetType(), typeof(TResponse));
object handler =
serviceProvider.GetService(handlerType) ??
throw new MediatorException($"Handler was not found for {request.GetType().Name}");
MethodInfo method = handlerType.GetMethod("Handle") ?? throw new MediatorException("Handle method was not found");
return await (Task<TResponse>)method.Invoke(handler, [request])!;
}
}
//注册服务, 自动bind:IRequestHandler<>, IRequestHandler<,>
public static class RegisterApplicationServices
{
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
{
services.AddTransient<IMediator, SimpleMediator>();
services.Scan(scan => scan
.FromAssembliesOf(typeof(RegisterApplicationServices))
.AddClasses(classes => classes.AssignableTo(typeof(IRequestHandler<>)))
.AsImplementedInterfaces()
.WithScopedLifetime()
.AddClasses(classes => classes.AssignableTo(typeof(IRequestHandler<,>)))
.AsImplementedInterfaces()
.WithScopedLifetime());
return services;
}
}
想要更深入地理解 整洁架构 的精髓和 Mediator模式 在 ASP.NET Core 中的妙用,最好的方式就是亲自观看课程视频,并结合项目代码进行动手实践。

希望这篇分享能对你的学习有所帮助。也欢迎你在 云栈社区 分享你的学习心得与实践经验。
封面图:

|