Cuture.Extensions.Modularity
1.1.6
.NET 5.0
This package targets .NET 5.0. The package is compatible with this framework or higher.
.NET Core 3.1
This package targets .NET Core 3.1. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Cuture.Extensions.Modularity --version 1.1.6
NuGet\Install-Package Cuture.Extensions.Modularity -Version 1.1.6
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.1.6" />
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.6
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Cuture.Extensions.Modularity, 1.1.6"
#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.1.6 // Install Cuture.Extensions.Modularity as a Cake Tool #tool nuget:?package=Cuture.Extensions.Modularity&version=1.1.6
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的模块实现方法相同;
- 可拓展的模块加载源,已实现基于
Type
、Assembly
、File
、Directory
的模块加载; IOptions<TOptions>
自动绑定;- 可集成其它模块系统的模块(如Abp),详见示例代码;
- 主项目只依赖
Microsoft.Extensions.DependencyInjection.Abstractions
,Hosting项目额外依赖Hosting.Abstractions
、Configuration.Binder
、Options
; - 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
即可。如果需要单独的配置,则可以单独实现IPreConfigureServices
、IConfigureServices
、IPostConfigureServices
、IOnPreApplicationInitialization
、IOnApplicationInitialization
、IOnPostApplicationInitialization
、IOnApplicationShutdown
以在对应的时机进行配置,或直接继承AppModule
并重写对应的方法;所有方法都有异步版本IPreConfigureServicesAsync
、IConfigureServicesAsync
、IPostConfigureServicesAsync
、IOnPreApplicationInitializationAsync
、IOnApplicationInitializationAsync
、IOnPostApplicationInitializationAsync
、IOnApplicationShutdownAsync
,或直接继承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:注册的方法,包括
Add
、TryAdd
、Replace
;
除示例的ExportServices
外,还有ExportSingletonServices
、ExportScopedServices
、ExportTransientServices
等多个拓展特性的重载实现;
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 Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. 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 was computed. 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 was computed. 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 is compatible. |
.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.
-
.NETCoreApp 3.1
-
.NETStandard 2.0
-
.NETStandard 2.1
-
net5.0
-
net6.0
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,354 | 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,243 | 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 |