当前位置: 首页 > news >正文

ABP vNext 框架功能模块 - 模块化(Modularity)

模块化(Modularity)

核心辅助类

  • AbpModule:所有模块的基类,定义模块生命周期方法。
  • DependsOnAttribute:声明模块依赖关系。
  • ModuleInitializer:模块初始化器(自动生成)。
  • IModuleContainer:模块容器,用于运行时获取所有加载的模块信息。
  • PreConfigureServicesAttribute:在模块ConfigureServices之前执行配置(用于提前注册服务)。

1. AbpModule

示例

[DependsOn(typeof(AbpAspNetCoreModule), typeof(SqlSugarModule))]
// 定义应用模块,继承AbpModule
public class MyApplicationModule : AbpModule
{// 配置服务(注册依赖、设置选项等)public override void ConfigureServices(ServiceConfigurationContext context){// 注册应用服务context.Services.AddScoped<IBookAppService, BookAppService>();// 配置对象映射Configure<AbpAutoMapperOptions>(options =>{options.AddMaps<MyApplicationModule>();});}// 应用初始化(配置中间件、数据库迁移等)public override void OnApplicationInitialization(ApplicationInitializationContext context){var app = context.GetApplicationBuilder();var env = context.GetEnvironment();// 开发环境启用异常页面if (env.IsDevelopment()){app.UseDeveloperExceptionPage();}// 配置路由app.UseRouting();app.UseEndpoints(endpoints =>{endpoints.MapControllers();});}
}

2. DependsOnAttribute

声明当前模块依赖的其他模块,确保依赖模块先于当前模块加载。

示例

// 声明当前模块依赖于EfCore模块和Identity模块
[DependsOn(typeof(AbpEfCoreModule),        // EF Core集成模块typeof(AbpIdentityAspNetCoreModule), // 身份认证模块typeof(MyDomainModule)          // 自定义领域模块
)]
public class MyApplicationModule : AbpModule
{// 模块逻辑...
}

说明:框架会先加载DependsOn中声明的模块,再加载当前模块,避免依赖顺序问题。

3. ModuleInitializer

模块初始化器(自动生成,无需手动编写),用于在应用启动时自动加载所有模块。

背景

ABP 在编译时会扫描所有AbpModule子类,自动生成ModuleInitializer类,其内部会创建模块容器并按依赖顺序初始化模块。

使用场景

开发者无需手动实例化模块,只需在Program.cs中通过AddApplicationAsync触发自动初始化:

var builder = WebApplication.CreateBuilder(args);
// 自动加载模块(内部使用ModuleInitializer)
await builder.AddApplicationAsync<MyWebModule>(); var app = builder.Build();
await app.InitializeApplicationAsync(); // 初始化所有模块
await app.RunAsync();

4. IModuleContainer

模块容器,用于在运行时获取已加载的所有模块信息(如模块类型、依赖关系等)。

示例

public class ModuleInfoService : ITransientDependency
{private readonly IModuleContainer _moduleContainer;public ModuleInfoService(IModuleContainer moduleContainer){_moduleContainer = moduleContainer;}public List<string> GetLoadedModuleNames(){// 获取所有已加载模块的类型名称return _moduleContainer.Modules.Select(m => m.Type.FullName).ToList();}public List<string> GetModuleDependencies(string moduleName){// 获取指定模块的依赖模块名称var module = _moduleContainer.Modules.FirstOrDefault(m => m.Type.Name == moduleName);return module?.Dependencies.Select(d => d.Type.Name).ToList() ?? new List<string>();}
}

用途:可用于调试(查看模块加载情况)或动态根据模块配置功能。

5. PreConfigureServicesAttribute

标记方法在模块ConfigureServices之前执行,用于提前注册服务或修改配置(如覆盖框架默认配置)。

示例

public class MyApplicationModule : AbpModule
{// 标记此方法在ConfigureServices之前执行[PreConfigureServices]public void PreConfigureServices(ServiceConfigurationContext context){// 提前配置JSON序列化(覆盖默认设置)context.Services.Configure<JsonOptions>(options =>{options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;options.JsonSerializerOptions.WriteIndented = true;});// 提前注册一个必须在其他服务之前初始化的组件context.Services.AddSingleton<IMyPreService, MyPreService>();}public override void ConfigureServices(ServiceConfigurationContext context){// 此处的配置会在PreConfigureServices之后执行context.Services.AddScoped<IBookAppService, BookAppService>();}
}

说明:适用于需要在模块正式配置服务前调整基础设置的场景(如序列化、跨域等)。

这些类共同构成了 ABP 的模块化系统,通过明确的依赖管理和生命周期控制,实现了应用的模块化拆分与灵活扩展。

http://www.hskmm.com/?act=detail&tid=38360

相关文章:

  • Devolutions Server权限提升漏洞分析与修复指南
  • AI股票预测分析报告 - 2025年10月24日 - 20:08:50
  • 题解:P14299 [JOI2023 预选赛 R2] 填充 / Painting
  • str.endswith() 类似的方法
  • 在 Astro 博客中优雅使用 51.la 统计数据
  • 2025.10.24博客
  • cgroup
  • 设计模式:代码界的 “光之巨人” 养成指南(附 C++ 实战)
  • 深度剖析OpenHarmony AI Engine:开发板端侧大模型推理插件机制全链路拆解 - 实践
  • Linux下的拼音输入法 (3)
  • P2606 [ZJOI2010] 排列计数 分析
  • 分治算法乱讲
  • 电动三轮车后桥改造添加动能回收实现无限续航的可能。
  • Claude Code配置记录
  • 视频融合平台EasyCVR在智慧工地中的应用:构建安全、智能、高效的“云上工地” - 实践
  • 股票操作统计分析报告 - 2025年10月24日
  • [HZOI] CSP-S模拟37 赛后总结
  • 24
  • 数字人:数字人公司排行榜及技术深度剖析
  • 【同余最短路】学习笔记
  • 数字人:数字人公司深度解析与未来展望
  • CSP/NOIP 复习:单调栈
  • 算法分析--生成排列
  • 三大安全认证授权协议深度对比:OAuth、OpenID Connect与SAML
  • 数字人公司:数字人新趋势技术驱动与市场前景解析
  • AI股票预测分析报告 - 2025年10月24日
  • 数据绑定相关概念理解
  • 数字人企业:数字人公司排行榜Top 3解析
  • (简记)(自用)线段树区间拆分时间复杂度证明
  • 数字人企业:数字人公司排行榜深度解析