找回密码
立即注册
搜索
热搜: Java Python Linux Go
发回帖 发新帖

2273

积分

0

好友

318

主题
发表于 昨天 13:29 | 查看: 5| 回复: 0

推荐一个关于 《ASP.NET Core 整洁架构实践》 的优质学习视频。

课程视频地址如下:

ASP.NET Core 整洁架构实践(中文语音版)
https://www.bilibili.com/video/BV1D5yJBSErH

ASP.NET Core Clean Architecture(英文原版)
https://www.bilibili.com/video/BV1u3CrBuEdk

Clean Architecture with ASP.NET Core 课程介绍幻灯片

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

Clean Coding Principles in C# 课程介绍幻灯片

在实践整洁架构时,虽然没有找到讲师 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 中的妙用,最好的方式就是亲自观看课程视频,并结合项目代码进行动手实践。

思考表情包

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

封面图
整洁架构 ASP.NET Core 中文语音宣传图




上一篇:如何在STM32MP257嵌入式平台为Linux 6.1.82开启pstore记录内核崩溃日志?
下一篇:尾部风险如何影响股票收益?Python量化分析揭示低相关性股票中的Alpha机会
您需要登录后才可以回帖 登录 | 立即注册

手机版|小黑屋|网站地图|云栈社区 ( 苏ICP备2022046150号-2 )

GMT+8, 2026-1-16 02:06 , Processed in 0.433659 second(s), 39 queries , Gzip On.

Powered by Discuz! X3.5

© 2025-2025 云栈社区.

快速回复 返回顶部 返回列表