Cuture.Extensions.Modularity 1.1.5

.NET 5.0 .NET Core 3.1 .NET Standard 2.0
There is a newer version of this package available.
See the version list below for details.
Install-Package Cuture.Extensions.Modularity -Version 1.1.5
dotnet add package Cuture.Extensions.Modularity --version 1.1.5
<PackageReference Include="Cuture.Extensions.Modularity" Version="1.1.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Cuture.Extensions.Modularity --version 1.1.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Cuture.Extensions.Modularity, 1.1.5"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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.1.5

// Install Cuture.Extensions.Modularity as a Cake Tool
#tool nuget:?package=Cuture.Extensions.Modularity&version=1.1.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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容器;(可选)
  • 继承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 Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
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.1.7 6,280 2/7/2022
1.1.6 2,409 1/6/2022
1.1.5 315 1/3/2022
1.1.4 390 12/30/2021
1.1.3 3,567 11/13/2021
1.1.2 8,047 7/31/2021
1.1.1 10,863 4/11/2021
1.1.0 219 4/8/2021
1.0.1 1,499 3/30/2021
1.0.0 3,480 2/22/2021
1.0.0-alpha0006 2,029 2/2/2021
1.0.0-alpha0005 690 1/27/2021
1.0.0-alpha0004 147 1/27/2021
1.0.0-alpha0003 166 1/26/2021
1.0.0-alpha0002 713 1/21/2021