From e499efcf821b10544eafd0dbff1dbc1876be95b9 Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Thu, 7 Jul 2022 23:41:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AD=90=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E4=BE=9D=E8=B5=96=E9=97=AE=E9=A2=98=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E5=BC=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/DependencyExtensions.cs | 3 +- .../ServiceCollectionApplicationExtensions.cs | 47 ++++++++++++++----- .../NetCoreApplicationContractsModule.cs | 8 +++- test/NetCore.HttpApi/NetCoreHttpApiModule.cs | 2 + 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/Token.Module/Extensions/DependencyExtensions.cs b/src/Token.Module/Extensions/DependencyExtensions.cs index 285a755..eb6a862 100644 --- a/src/Token.Module/Extensions/DependencyExtensions.cs +++ b/src/Token.Module/Extensions/DependencyExtensions.cs @@ -12,11 +12,12 @@ public static class DependencyExtensions var assemblies = tokenModules.Select(x => x.GetType().Assembly).Distinct() .SelectMany(x => x.GetTypes()); + // 过滤程序集 var types = assemblies .Where(type => typeof(ISingletonDependency).IsAssignableFrom(type) || typeof(IScopedDependency).IsAssignableFrom(type) || typeof(ITransientDependency).IsAssignableFrom(type)); - + // 注入 foreach (var t in types) { var interfaces = t.GetInterfaces().Where(x => x.Name.EndsWith(t.Name))?.FirstOrDefault(); diff --git a/src/Token.Module/Extensions/ServiceCollectionApplicationExtensions.cs b/src/Token.Module/Extensions/ServiceCollectionApplicationExtensions.cs index f5678ac..be7b510 100644 --- a/src/Token.Module/Extensions/ServiceCollectionApplicationExtensions.cs +++ b/src/Token.Module/Extensions/ServiceCollectionApplicationExtensions.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using System.Reflection; using Microsoft.Extensions.DependencyInjection; using System.Threading.Tasks; @@ -20,20 +21,13 @@ public static class ServiceCollectionApplicationExtensions { var types = new List(); var type = typeof(TModule); - var attributes = type.GetCustomAttributes().OfType() - .SelectMany(x => x.Type); + await GetModuleTypeAsync(type, types); - var module = type.Assembly.CreateInstance(type.FullName, true) as ITokenModule; - types.Add(module); - await module.ConfigureServicesAsync(services); - foreach (var t in attributes) + foreach (var t in types.Distinct()) { - if (t.Assembly.CreateInstance(t.FullName, true) is not ITokenModule ts) - continue; - - types.Add(ts); - await ts.ConfigureServicesAsync(services); + await t.ConfigureServicesAsync(services); } + services.AddSingleton(types); if (isAutoInject) { @@ -41,6 +35,35 @@ public static class ServiceCollectionApplicationExtensions } } + private static async Task GetModuleTypeAsync(Type type,List 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); + + // 获取DependOn特性注入的模块 + 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); + } + } + /// /// 初始化Application /// diff --git a/test/NetCore.Application.Contracts/NetCoreApplicationContractsModule.cs b/test/NetCore.Application.Contracts/NetCoreApplicationContractsModule.cs index 777e577..eba1783 100644 --- a/test/NetCore.Application.Contracts/NetCoreApplicationContractsModule.cs +++ b/test/NetCore.Application.Contracts/NetCoreApplicationContractsModule.cs @@ -1,8 +1,12 @@ -using Token.Module; +using Microsoft.Extensions.DependencyInjection; +using Token.Module; namespace NetCore.Application.Contracts; public class NetCoreApplicationContractsModule : TokenModule { - + public override void ConfigureServices(IServiceCollection services) + { + + } } \ No newline at end of file diff --git a/test/NetCore.HttpApi/NetCoreHttpApiModule.cs b/test/NetCore.HttpApi/NetCoreHttpApiModule.cs index b86ac89..ee40535 100644 --- a/test/NetCore.HttpApi/NetCoreHttpApiModule.cs +++ b/test/NetCore.HttpApi/NetCoreHttpApiModule.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models; using NetCore.Domain; using Token.Module; +using Token.Module.Attributes; namespace NetCore.HttpApi; @@ -16,6 +17,7 @@ public class NetCoreHttpApiModule : TokenModule ConfigCors(services); } + private void ConfigCors(IServiceCollection services) { services.AddCors(options => -- Gitee From 787b8c7b53e9be2a58f5bdf327b68b7b8fa9b3fd Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Thu, 7 Jul 2022 23:47:12 +0800 Subject: [PATCH 2/2] =?UTF-8?q?nuget=E7=89=88=E6=9C=AC=E6=9B=B4=E6=96=B0?= 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 3714e76..34870de 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 - 修复Configure无法问题 + 修复子模块未依赖问题 logo.png - 1.1.7 + 1.1.8 -- Gitee