diff --git a/Falcon.SugarApi.Test/ModuleRegisterTest.cs b/Falcon.SugarApi.Test/ModuleRegisterTest.cs new file mode 100644 index 0000000..5766078 --- /dev/null +++ b/Falcon.SugarApi.Test/ModuleRegisterTest.cs @@ -0,0 +1,35 @@ +using Falcon.SugarApi.ApiDefinistions; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Falcon.SugarApi.Test +{ + /// + /// 模块注册测试 + /// + [TestClass] + public class ModuleRegisterTest + { + /// + /// 注册测试 + /// + [TestMethod] + public void RegisterTest() { + var services = new ServiceCollection() as IServiceCollection; + var cb = new ConfigurationBuilder(); + var config = cb.Build() as IConfiguration; + ModuleRegister.ModuleServiceRegistration(services,"aaa",config); + var ab = new ApplicationBuilder(services.BuildServiceProvider()); + ModuleRegister.ApplicationBuilder(ab,"aaa",config); + + + } + } +} diff --git a/Falcon.SugarApi/ApiDefinistions/IModuleRegister.cs b/Falcon.SugarApi/ApiDefinistions/IModuleRegister.cs new file mode 100644 index 0000000..4c1b3dd --- /dev/null +++ b/Falcon.SugarApi/ApiDefinistions/IModuleRegister.cs @@ -0,0 +1,31 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace Falcon.SugarApi.ApiDefinistions +{ + /// + /// 模块注册接口 + /// + public interface IModuleRegister + { + /// + /// 服务注册 + /// + /// 服务结合 + /// 配置 + /// 服务集合 + public IServiceCollection ServiceRegister(IServiceCollection services,IConfiguration configuration); + + /// + /// 应用注册 + /// + /// 应用创建器 + /// 配置 + /// 应用创建器 + public IApplicationBuilder ApplicationRegister(IApplicationBuilder application,IConfiguration configuration); + } +} diff --git a/Falcon.SugarApi/ApiDefinistions/ModuleRegister.cs b/Falcon.SugarApi/ApiDefinistions/ModuleRegister.cs new file mode 100644 index 0000000..d640165 --- /dev/null +++ b/Falcon.SugarApi/ApiDefinistions/ModuleRegister.cs @@ -0,0 +1,63 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace Falcon.SugarApi.ApiDefinistions +{ + /// + /// 模块注册 + /// + public static class ModuleRegister + { + /// + /// 模块服务注册 + /// + /// 服务集合 + /// 模块名称 + /// 配置 + /// 服务集合 + /// + public static IServiceCollection ModuleServiceRegistration(IServiceCollection services,string moduleName,IConfiguration configuration) { + foreach(var mr in GetModuleRegister(moduleName)) { + mr.ServiceRegister(services,configuration); + } + return services; + } + + /// + /// 应用注册 + /// + /// 应用创建器 + /// 模块名称 + /// 配置 + /// 应用创建器 + public static IApplicationBuilder ApplicationBuilder(IApplicationBuilder application,string moduleName,IConfiguration configuration) { + foreach(var mr in GetModuleRegister(moduleName)) { + mr.ApplicationRegister(application,configuration); + } + return application; + } + + private static IEnumerable GetModuleRegister(string moduleName) { + Assembly ass; + try { + ass = Assembly.Load(moduleName); + } + catch(Exception ex) { + throw new Exception($"获取程序集{moduleName}错误!",ex); + } + var types = ass.GetTypes(); + foreach(var type in types) { + if(type.GetInterface(typeof(IModuleRegister).FullName) != null) { + var obj = type.Assembly.CreateInstance(type.FullName); + if(obj is IModuleRegister mr) { + yield return mr; + } + } + } + } + } +}