Files
Yi.Framework/Yi.Doc.Md/01.框架快速开始教程/04.结构/03.模块结构.md
2023-12-15 18:27:22 +08:00

1.9 KiB
Raw Blame History

简介

通常在Asp.NetCore中容器组装过程 与 管道模型组装 过程 会将启动类文件变的非常长,同时也需要明确各个模块的依赖关系 例如: 我们需要仓储的功能但是仓储的实现需要依赖Sqlsugar 老的引入写法:

service.AddUow();
service.AddSqlsugar();
......
var app=service.Build();
app.UseSqlsugar();
......

这个文件会变得非常长,同时如果有顺序依赖关系的模块,还需按顺序组装 例如: 在Asp.NetCore,我们只有先鉴权才能进行授权操作 当模块越来越多,我们维护起来将越来越困难,所以引入了模块化功能

使用

每一个类库都可以有自己的模块化文件,我们通常命名为类库全名+Module 例如:Yi.Template.Application的模块类叫做YiTemplateApplicationModule

另外,该模块类实现AbpModule基类 ConfigureServices:用来配置容器服务 OnApplicationInitialization用来配置管道模型

Abp内置DependsOn特性标签,可进行维护各个模块之间的依赖关系

完整例子

创建模块化文件:

using Volo.Abp.Modularity;
using Yi.Abp.Domain;
using Yi.Abp.SqlSugarCore;
using Yi.Framework.Bbs.SqlSugarCore;
using Yi.Framework.Mapster;
using Yi.Framework.Rbac.SqlSugarCore;
using Yi.Framework.SqlSugarCore;

namespace Yi.Abp.SqlsugarCore
{
    [DependsOn(
        typeof(YiAbpDomainModule),

        typeof(YiFrameworkRbacSqlSugarCoreModule),
        typeof(YiFrameworkBbsSqlSugarCoreModule),

        typeof(YiFrameworkMapsterModule),
        typeof(YiFrameworkSqlSugarCoreModule)
        )]
    public class YiAbpSqlSugarCoreModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.AddYiDbContext<YiDbContext>();
        }
    }
}

在启动管道模型组装文件使用入口模块: