From a17d7697e62705bf1981d23522fa49c06cff9082 Mon Sep 17 00:00:00 2001
From: falcon <9504402@qq.com>
Date: Fri, 5 May 2023 13:51:50 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=A8=A1=E5=9D=97=E6=B3=A8?=
=?UTF-8?q?=E5=86=8C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Falcon.SugarApi.Test/ModuleRegisterTest.cs | 35 +++++++++++
.../ApiDefinistions/IModuleRegister.cs | 31 +++++++++
.../ApiDefinistions/ModuleRegister.cs | 63 +++++++++++++++++++
3 files changed, 129 insertions(+)
create mode 100644 Falcon.SugarApi.Test/ModuleRegisterTest.cs
create mode 100644 Falcon.SugarApi/ApiDefinistions/IModuleRegister.cs
create mode 100644 Falcon.SugarApi/ApiDefinistions/ModuleRegister.cs
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;
+ }
+ }
+ }
+ }
+ }
+}