From 9caf1770ed2c2c01acbbe8d066d3f37566a213ae Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Fri, 8 Jul 2022 00:04:42 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E5=BE=AA=E7=8E=AF=E4=BE=9D=E8=B5=96=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/ServiceCollectionApplicationExtensions.cs | 2 -- src/Token.Module/Token.Module.csproj | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Token.Module/Extensions/ServiceCollectionApplicationExtensions.cs b/src/Token.Module/Extensions/ServiceCollectionApplicationExtensions.cs index be7b510..6e524b9 100644 --- a/src/Token.Module/Extensions/ServiceCollectionApplicationExtensions.cs +++ b/src/Token.Module/Extensions/ServiceCollectionApplicationExtensions.cs @@ -51,14 +51,12 @@ public static class ServiceCollectionApplicationExtensions var attributes = type.GetCustomAttributes().OfType() .SelectMany(x => x.Type).Where(x=>iTokenModule.IsAssignableFrom(x)); - foreach (var t in attributes) { ITokenModule module = t.Assembly.CreateInstance(t?.FullName, true) as ITokenModule; if(module==null) continue; - types.Add(module); // 可能存在循环依赖的问题 await GetModuleTypeAsync(t, types); } diff --git a/src/Token.Module/Token.Module.csproj b/src/Token.Module/Token.Module.csproj index 34870de..63e587f 100644 --- a/src/Token.Module/Token.Module.csproj +++ b/src/Token.Module/Token.Module.csproj @@ -10,9 +10,9 @@ true https://github.com/239573049/token-module https://github.com/239573049/token-module - 修复子模块未依赖问题 + 修复子模块循环注入问题 logo.png - 1.1.8 + 1.1.9 -- Gitee From 4bcdffe94e6a87266f2b6a860c603ab842dbd8ee Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Sat, 9 Jul 2022 02:34:50 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Module=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Attributes/RunOrderAttribute.cs | 15 ++++ .../Extensions/DependencyExtensions.cs | 2 +- .../ServiceCollectionApplicationExtensions.cs | 76 +++++++++++++------ src/Token.Module/ITokenModule.cs | 2 +- src/Token.Module/TokenModule.cs | 2 +- test/NetCore.Domain/NetCoreDomainModule.cs | 2 + test/NetCoreTest/Program.cs | 2 +- 7 files changed, 75 insertions(+), 26 deletions(-) create mode 100644 src/Token.Module/Attributes/RunOrderAttribute.cs diff --git a/src/Token.Module/Attributes/RunOrderAttribute.cs b/src/Token.Module/Attributes/RunOrderAttribute.cs new file mode 100644 index 0000000..a2ab38d --- /dev/null +++ b/src/Token.Module/Attributes/RunOrderAttribute.cs @@ -0,0 +1,15 @@ +using System; + +namespace Token.Module.Attributes; + + +[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] +public class RunOrderAttribute :Attribute +{ + public int Order; + + public RunOrderAttribute(int order) + { + Order = order; + } +} \ No newline at end of file diff --git a/src/Token.Module/Extensions/DependencyExtensions.cs b/src/Token.Module/Extensions/DependencyExtensions.cs index eb6a862..95ad787 100644 --- a/src/Token.Module/Extensions/DependencyExtensions.cs +++ b/src/Token.Module/Extensions/DependencyExtensions.cs @@ -7,7 +7,7 @@ namespace Token.Module.Extensions; public static class DependencyExtensions { - public static void AddAutoInject(this IServiceCollection services, List tokenModules) + public static void AddAutoInject(this IServiceCollection services, IEnumerable tokenModules) { var assemblies = tokenModules.Select(x => x.GetType().Assembly).Distinct() .SelectMany(x => x.GetTypes()); diff --git a/src/Token.Module/Extensions/ServiceCollectionApplicationExtensions.cs b/src/Token.Module/Extensions/ServiceCollectionApplicationExtensions.cs index 6e524b9..0b1e5c9 100644 --- a/src/Token.Module/Extensions/ServiceCollectionApplicationExtensions.cs +++ b/src/Token.Module/Extensions/ServiceCollectionApplicationExtensions.cs @@ -11,67 +11,99 @@ namespace Token.Module.Extensions; public static class ServiceCollectionApplicationExtensions { + /// + /// 默认运行顺序 + /// + private static int _defaultOrder = 1; + + public static void SetDefaultOrder(int order) + { + _defaultOrder = order; + } + /// /// 初始化Service /// /// /// 是否自动依赖注入 /// - public static async Task AddTagApplication(this IServiceCollection services,bool isAutoInject=true) where TModule : ITokenModule + public static async Task AddModuleApplication(this IServiceCollection services, bool isAutoInject = true) + where TModule : ITokenModule { - var types = new List(); + var types = new List>(); var type = typeof(TModule); await GetModuleTypeAsync(type, types); - foreach (var t in types.Distinct()) + var modules = types.OrderBy(x => x.Item2).Select(x => x.Item1).Distinct(); + + var tokenModules = modules as ITokenModule[] ?? modules.ToArray(); + if (isAutoInject) + { + services.AddAutoInject(tokenModules); + } + + + foreach (var t in tokenModules) { await t.ConfigureServicesAsync(services); } - + services.AddSingleton(types); - if (isAutoInject) + } + + /// + /// 初始化Application + /// + /// + public static void InitializeApplication(this IApplicationBuilder app) + { + var types = app.ApplicationServices.GetService>>(); + + var modules = types?.OrderBy(x => x.Item2).Select(x => x.Item1); + + if (modules == null) + return; + + async void Action(ITokenModule x) => await x.OnApplicationShutdownAsync(app); + + foreach (var module in modules) { - services.AddAutoInject(types); + Action(module); } } - private static async Task GetModuleTypeAsync(Type type,List types) + private static async Task GetModuleTypeAsync(Type type, ICollection> types) { var iTokenModule = typeof(ITokenModule); if (!iTokenModule.IsAssignableFrom(type)) { return; } + // 通过放射创建一个对象并且回调方法 ITokenModule typeInstance = type.Assembly.CreateInstance(type.FullName, true) as ITokenModule; - if (typeInstance != null) types.Add(typeInstance); + if (typeInstance != null) types.Add(new Tuple(typeInstance, GetRunOrder(type))); // 获取DependOn特性注入的模块 var attributes = type.GetCustomAttributes().OfType() - .SelectMany(x => x.Type).Where(x=>iTokenModule.IsAssignableFrom(x)); - + .SelectMany(x => x.Type).Where(x => iTokenModule.IsAssignableFrom(x)); + foreach (var t in attributes) { - ITokenModule module = t.Assembly.CreateInstance(t?.FullName, true) as ITokenModule; - if(module==null) + ITokenModule? module = t.Assembly.CreateInstance(t?.FullName, true) as ITokenModule; + if (module == null) continue; // 可能存在循环依赖的问题 await GetModuleTypeAsync(t, types); } } - - /// - /// 初始化Application - /// - /// - public static void InitializeApplication(this IApplicationBuilder app) - { - var tag = app.ApplicationServices.GetService>(); - async void Action(ITokenModule x) => await x.OnApplicationShutdownAsync(app); + private static int GetRunOrder(Type type) + { + var runOrder = type.GetCustomAttribute(); - tag.ForEach(Action); + return runOrder?.Order ?? _defaultOrder++; } } \ No newline at end of file diff --git a/src/Token.Module/ITokenModule.cs b/src/Token.Module/ITokenModule.cs index d817be1..f244362 100644 --- a/src/Token.Module/ITokenModule.cs +++ b/src/Token.Module/ITokenModule.cs @@ -9,7 +9,7 @@ public interface ITokenModule Task ConfigureServicesAsync(IServiceCollection services); void ConfigureServices(IServiceCollection services); - + Task OnApplicationShutdownAsync(IApplicationBuilder app); void OnApplicationShutdown(IApplicationBuilder app); diff --git a/src/Token.Module/TokenModule.cs b/src/Token.Module/TokenModule.cs index 1e1df13..7248950 100644 --- a/src/Token.Module/TokenModule.cs +++ b/src/Token.Module/TokenModule.cs @@ -8,7 +8,7 @@ namespace Token.Module; public abstract class TokenModule : ITokenModule { private IServiceCollection _serviceCollection; - + public virtual Task ConfigureServicesAsync(IServiceCollection services) { _serviceCollection = services; diff --git a/test/NetCore.Domain/NetCoreDomainModule.cs b/test/NetCore.Domain/NetCoreDomainModule.cs index fc35f82..5a6d46b 100644 --- a/test/NetCore.Domain/NetCoreDomainModule.cs +++ b/test/NetCore.Domain/NetCoreDomainModule.cs @@ -3,6 +3,8 @@ using Token.Module.Attributes; namespace NetCore.Domain; +[RunOrder(1)] +[DependOn] public class NetCoreDomainModule : TokenModule { } \ No newline at end of file diff --git a/test/NetCoreTest/Program.cs b/test/NetCoreTest/Program.cs index d68e3f1..372c726 100644 --- a/test/NetCoreTest/Program.cs +++ b/test/NetCoreTest/Program.cs @@ -4,7 +4,7 @@ using NetCoreTest; var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); -builder.Services.AddTagApplication(); +builder.Services.AddModuleApplication(); var app = builder.Build(); app.InitializeApplication(); -- Gitee From 0ea276313a414d4336cad9830b631cd1a5352482 Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Sat, 9 Jul 2022 02:37:06 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0nuget=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Token.Module/Token.Module.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Token.Module/Token.Module.csproj b/src/Token.Module/Token.Module.csproj index 63e587f..626cd1a 100644 --- a/src/Token.Module/Token.Module.csproj +++ b/src/Token.Module/Token.Module.csproj @@ -10,9 +10,9 @@ true https://github.com/239573049/token-module https://github.com/239573049/token-module - 修复子模块循环注入问题 + 新增模块执行顺序实现 logo.png - 1.1.9 + 1.2.0 -- Gitee