ABP - 模块化(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 的模块化系统,通过明确的依赖管理和生命周期控制,实现了应用的模块化拆分与灵活扩展。

posted @ 2025-10-24 20:15  【唐】三三  阅读(3)  评论(0)    收藏  举报