当前位置: 首页 > 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.rkmt.cn/news/29678.html

相关文章:

  • ABP vNext 框架功能模块
  • 题解:P14299 [JOI2023 预选赛 R2] 填充 / Painting
  • Devolutions Server权限提升漏洞分析与修复指南
  • 在 Astro 博客中优雅使用 51.la 统计数据
  • 2025.10.24博客
  • 深度剖析OpenHarmony AI Engine:开发板端侧大模型推理插件机制全链路拆解 - 实践
  • ESP32-S3入门第七天:UART串口通信与设备交互 - 教程
  • 数据绑定相关概念理解
  • 数字人企业:数字人公司排行榜Top 3解析
  • 数字人企业:数字人公司技术驱动的三大标杆
  • Linux下的拼音输入法 (2)
  • 拉格朗日插值优化DP
  • 容斥练习笔记
  • 数字人企业:推荐数字人TOP3公司
  • 数字人平台:重点推荐优质数字人公司
  • 深入解析:【Java系列课程Java学前须知】第3课 JDK,JVM,JRE的区别和优缺
  • 395.至少有K个重复字符的最长字串
  • 详细介绍:云手机远程控制的作用
  • 10.24模拟赛
  • 2025.10.24NOIP
  • writing sentences
  • 小程序 访问第三方网页
  • 国产开源数据库调研项目的LaTeX专业排版实践
  • CompletableFuture串联多个异步任务实践
  • 城市基础设施安全运行监管平台
  • ZR 2025 NOIP 二十连测 Day 7
  • CSP-S 37
  • CSharp: word,excel,powerpoint convert to pdf,hrml etc using Aspose.Office
  • Offsec Nibbles CTF 实战解析:PostgreSQL漏洞利用与权限提升
  • MySQLdump 常用参数说明 - 实践