Cuture.Extensions.Modularity 1.2.0

dotnet add package Cuture.Extensions.Modularity --version 1.2.0                
NuGet\Install-Package Cuture.Extensions.Modularity -Version 1.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cuture.Extensions.Modularity" Version="1.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Cuture.Extensions.Modularity --version 1.2.0                
#r "nuget: Cuture.Extensions.Modularity, 1.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Cuture.Extensions.Modularity as a Cake Addin
#addin nuget:?package=Cuture.Extensions.Modularity&version=1.2.0

// Install Cuture.Extensions.Modularity as a Cake Tool
#tool nuget:?package=Cuture.Extensions.Modularity&version=1.2.0                

Cuture.Extensions.Modularity

1. Intro

围绕 Microsoft.Extensions.DependencyInjection.Abstractions 为核心的.Net模块化开发库.

2. Features

  • 支持异步的模块配置方法;
  • 支持基于特性标注的服务自动注入(默认不支持基于继承的服务自动注入);
  • 基本和Abp的模块实现方法相同;
  • 可拓展的模块加载源,已实现基于TypeAssemblyFileDirectory的模块加载;
  • IOptions<TOptions>自动绑定;
  • 可集成其它模块系统的模块(如Abp),详见示例代码;
  • 主项目只依赖Microsoft.Extensions.DependencyInjection.Abstractions,Hosting项目额外依赖Hosting.AbstractionsConfiguration.BinderOptions
  • Mermaid生成工具,方便查看模块依赖关系;

Nuget包列表

Package Description
Cuture.Extensions.Modularity 模块化的核心库
Cuture.Extensions.Modularity.Hosting 对Host的模块化支持库,用于在通用主机中加载模块

3. 如何使用

3.1 安装Nuget

Install-Package Cuture.Extensions.Modularity

3.2 实现一个模块

[DependsOn(
    typeof(DependsSampleModule1),
    typeof(DependsSampleModule2)
    )]  //定义依赖的模块
[AutoRegisterServicesInAssembly]
public class SampleModule : AppModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        //进行模块需要的配置
    }
}
  • [DependsOn]:声明此模块依赖的模块,加载顺序与声明顺序相同;(可选)
  • [AutoRegisterServicesInAssembly]:有此特性的模块将会自动将所在程序集中使用ExportServices标记的导出服务注入到DI容器;(可选)
    • 1.1.6之后默认自动注册,使用[DisableAssemblyServicesRegister]特性进行手动关闭
  • 继承AppModule:标准的模块只需要继承IAppModule即可。如果需要单独的配置,则可以单独实现IPreConfigureServicesIConfigureServicesIPostConfigureServicesIOnPreApplicationInitializationIOnApplicationInitializationIOnPostApplicationInitializationIOnApplicationShutdown以在对应的时机进行配置,或直接继承AppModule并重写对应的方法;所有方法都有异步版本IPreConfigureServicesAsyncIConfigureServicesAsyncIPostConfigureServicesAsyncIOnPreApplicationInitializationAsyncIOnApplicationInitializationAsyncIOnPostApplicationInitializationAsyncIOnApplicationShutdownAsync,或直接继承AsyncAppModule

3.3 直接通过DI容器使用模块

可能需要安装Microsoft.Extensions.DependencyInjection包,视主项目而定
var services = new ServiceCollection();

//加载模块
services.LoadModule<SampleModule>() //直接从类型加载
        .LoadModuleFile(modulePath) //从文件加载
        .LoadModuleDirectory(source =>
        {
            source.SearchDepth = 5;    //设置文件夹搜索深度
        }, moduleDirectory)  //从文件夹加载
        .ModuleLoadComplete()   //必须调用此方法,以确认模块加载完成

using (var serviceProvider = services.BuildServiceProvider())
{
    //必须调用此方法,以初始化模块
    serviceProvider.InitializationModulesWithOutHostLifetime();

    //这里使用初始化了模块的serviceProvider

    //关闭模块
    serviceProvider.ShutdownModules();
}

Note:

  • 直接使用DI容器时,不会在控制台等关闭时,自动调用模块结束方法(使用Host时会),需要手动调用;

3.4 通过通用主机使用模块 (Console、Asp .net Core...)

1. 安装Hosting拓展库
Install-Package Cuture.Extensions.Modularity.Hosting
2. 在主机构建时配置模块加载
Host.CreateDefaultBuilder(args)
    .LoadModule<SampleModule>() //直接从类型加载
    .LoadModuleFile(modulePath) //从文件加载
    .LoadModuleDirectory(source =>
    {
        source.SearchDepth = 5;    //设置文件夹搜索深度
    }, moduleDirectory)  //从文件夹加载
    .UseConsoleLifetime()
    .InitializationModules()    //必须调用此方法,以初始化模块
    .Run();
  • 也可以通过配置主机DI容器的方法来使用

更多细节详见示例代码;

3.5 服务导出

  • 需要为类型所在程序集模块标记特性[AutoRegisterServicesInAssembly]
[ExportServices(ServiceLifetime.Singleton, AddDIMode.Replace, typeof(IHello))]
public class Hello : IHello
{
    public string SayHello()
    {
        return "Hello";
    }
}

示例代码会自动将Hello类型以单例模式注册为IHello服务,并在注入时使用Replace方法。

参数:

  • ServiceLifetime:注册的生命周期类型;
  • AddDIMode:注册的方法,包括AddTryAddReplace

除示例的ExportServices外,还有ExportSingletonServicesExportScopedServicesExportTransientServices等多个拓展特性的重载实现;

IOptions<TOptions>自动绑定

  • 自动查找模块中继承了IOptions<TOptions>的类;
  • 使用其完整名称为路径,在IConfiguration查找节点,并绑定值;
  • A 类命名空间为 B.C.D.E.F ,则IConfiguration查找路径为 B:C:D:E:F:A
  • Note: 构建过程中必须有可访问的IConfiguration !!! 详见示例项目;

示例配置代码:

  • 在构建中调用 AutoBindModuleOptions() 方法即可;
Host.CreateDefaultBuilder(args)
    .ConfigureHostConfiguration(builder => builder.AddJsonFile("appsettings.Development.json"))
    .LoadModule<HostSampleModule>()
    .AutoBindModuleOptions()    //自动使用 IConfiguration 绑定模块中继承了 IOptions<TOptions> 的类
    .UseConsoleLifetime()
    .InitializationModules()
    .Run();

集成其它模块系统的模块

参考示例项目OtherModuleSystemAdaptSample

其它

获取依赖的Mermaid字符串

var abpModuleDescriptors = AppModuleDependencyUtil.FindAllDependedModuleDescriptors(typeof(XXXModule));
var mermaidString = abpModuleDescriptors.ToMermaidString();

然后将mermaidString复制到支持mermaid的编辑器就能查看模块依赖关系了。

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Cuture.Extensions.Modularity:

Package Downloads
Cuture.Extensions.Modularity.Hosting

a library for modular develop with Cuture.Extensions.Modularity and Microsoft.Extensions.Hosting.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 23,128 1/11/2024
1.1.13 44,469 8/5/2022
1.1.7 13,495 2/7/2022
1.1.6 3,126 1/6/2022
1.1.5 1,061 1/3/2022
1.1.4 1,125 12/30/2021
1.1.3 4,322 11/13/2021
1.1.2 8,888 7/31/2021
1.1.1 11,632 4/11/2021
1.1.0 913 4/8/2021
1.0.1 2,189 3/30/2021
1.0.0 4,237 2/22/2021
1.0.0-alpha0006 2,593 2/2/2021
1.0.0-alpha0005 1,241 1/27/2021
1.0.0-alpha0004 766 1/27/2021
1.0.0-alpha0003 766 1/26/2021
1.0.0-alpha0002 1,293 1/21/2021