From 0672698ba7cbb01b036d852657a7d847c1fe4ccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= Date: Sun, 9 Oct 2022 17:32:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=89=B9=E6=80=A7=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E4=BE=9D=E8=B5=96=E6=B3=A8=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/TestController.cs | 1 + .../Controllers/UserController.cs | 1 + .../Yi.Framework.ApiMicroservice/Program.cs | 5 + .../Attribute/AppServiceAttribute.cs | 30 ++++++ .../Attribute}/LogAttribute.cs | 5 +- .../Yi.Framework.Repository/Repository.cs | 2 + .../Yi.Framework.Service/BaseService.cs | 2 + .../Yi.Framework.Task/HttpJob.cs | 6 +- .../AttributeExtend/GlobalLogAttribute.cs | 1 + .../MiddlewareExtend/AutoIocExtend.cs | 97 +++++++++++++++++++ .../Utility/CustomAutofacModule.cs | 7 +- 11 files changed, 149 insertions(+), 8 deletions(-) create mode 100644 Yi.Framework.Net6/Yi.Framework.Common/Attribute/AppServiceAttribute.cs rename Yi.Framework.Net6/{Yi.Framework.WebCore/AttributeExtend => Yi.Framework.Common/Attribute}/LogAttribute.cs (88%) create mode 100644 Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/AutoIocExtend.cs diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs index 186dc8d7..603d7eb4 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Yi.Framework.Common.Attribute; using Yi.Framework.Common.Const; using Yi.Framework.Common.Models; using Yi.Framework.Core; diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs index aaafe4b7..8b923b7d 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Yi.Framework.Common.Attribute; using Yi.Framework.Common.Const; using Yi.Framework.Common.Enum; using Yi.Framework.Common.Helper; diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs index 0f4f6f01..7949d9c1 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs @@ -31,10 +31,15 @@ builder.Host.ConfigureAppConfiguration((hostBuilderContext, configurationBuilder builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); builder.Host.ConfigureContainer(containerBuilder => { + #region //交由Module依赖注入 #endregion containerBuilder.RegisterModule(); + #region + //使用AppService特性优雅的进行自动依赖注入,仓储与基类服务便是使用该种方式自动注入 + #endregion + containerBuilder.AddAutoIocService("Yi.Framework.Repository", "Yi.Framework.Service"); }); builder.Host.ConfigureLogging(loggingBuilder => { diff --git a/Yi.Framework.Net6/Yi.Framework.Common/Attribute/AppServiceAttribute.cs b/Yi.Framework.Net6/Yi.Framework.Common/Attribute/AppServiceAttribute.cs new file mode 100644 index 00000000..5b833933 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Common/Attribute/AppServiceAttribute.cs @@ -0,0 +1,30 @@ +锘 +using System; + +namespace Yi.Framework.Common.Attribute +{ + /// + /// 鍙傝冨湴鍧锛歨ttps://www.cnblogs.com/kelelipeng/p/10643556.html + /// 1銆乕AppService]锛氳嚜鍔ㄥ幓鎵炬帴鍙o紝濡傛灉瀛樺湪灏辨槸鎺ュ彛锛屽鏋滀笉瀛樺湪灏辨槸鏈韩 + /// 2銆乕AppService(ServiceType = typeof(娉ㄥ唽鎶借薄鎴栬呮帴鍙f垨鑰呮湰韬))]锛屾墜鍔ㄥ幓娉ㄥ唽锛屾斁type鍗冲彲 + /// + [AttributeUsage(AttributeTargets.Class, Inherited = false)] + public class AppServiceAttribute : System.Attribute + { + /// + /// 鏈嶅姟澹版槑鍛ㄦ湡 + /// 涓嶇粰榛樿鍊肩殑璇濇敞鍐岀殑鏄綔鐢ㄥ煙 + /// + public LifeTime ServiceLifetime { get; set; } = LifeTime.Scoped; + /// + /// 鎸囧畾鏈嶅姟绫诲瀷 + /// + public Type ServiceType { get; set; } + + } + + public enum LifeTime + { + Transient, Scoped, Singleton + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/AttributeExtend/LogAttribute.cs b/Yi.Framework.Net6/Yi.Framework.Common/Attribute/LogAttribute.cs similarity index 88% rename from Yi.Framework.Net6/Yi.Framework.WebCore/AttributeExtend/LogAttribute.cs rename to Yi.Framework.Net6/Yi.Framework.Common/Attribute/LogAttribute.cs index 2ead4163..58a3571a 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/AttributeExtend/LogAttribute.cs +++ b/Yi.Framework.Net6/Yi.Framework.Common/Attribute/LogAttribute.cs @@ -1,5 +1,4 @@ 锘縰sing Microsoft.AspNetCore.Mvc.Filters; -using Microsoft.IdentityModel.JsonWebTokens; using System; using System.Collections.Generic; using System.Linq; @@ -8,10 +7,10 @@ using System.Threading.Tasks; using Yi.Framework.Common.Const; using Yi.Framework.Common.Enum; -namespace Yi.Framework.WebCore.AttributeExtend +namespace Yi.Framework.Common.Attribute { [AttributeUsage(AttributeTargets.Method)] - public class LogAttribute : Attribute + public class LogAttribute : System.Attribute { /// /// 鎿嶄綔绫诲瀷 diff --git a/Yi.Framework.Net6/Yi.Framework.Repository/Repository.cs b/Yi.Framework.Net6/Yi.Framework.Repository/Repository.cs index bd689814..1bf2dc4e 100644 --- a/Yi.Framework.Net6/Yi.Framework.Repository/Repository.cs +++ b/Yi.Framework.Net6/Yi.Framework.Repository/Repository.cs @@ -1,6 +1,7 @@ 锘縰sing SqlSugar; using System.Data; using System.Linq.Expressions; +using Yi.Framework.Common.Attribute; using Yi.Framework.Common.Models; using Yi.Framework.Model.Models; using Yi.Framework.Model.Query; @@ -12,6 +13,7 @@ namespace Yi.Framework.Repository /// 浠撳偍妯″紡 /// /// + [AppService] public class Repository : SimpleClient, IRepository where T : class, new() { public ISugarQueryable _DbQueryable { get { return base.Context.Queryable(); } set { } } diff --git a/Yi.Framework.Net6/Yi.Framework.Service/BaseService.cs b/Yi.Framework.Net6/Yi.Framework.Service/BaseService.cs index 26f11ef6..f46d6076 100644 --- a/Yi.Framework.Net6/Yi.Framework.Service/BaseService.cs +++ b/Yi.Framework.Net6/Yi.Framework.Service/BaseService.cs @@ -3,12 +3,14 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Yi.Framework.Common.Attribute; using Yi.Framework.Interface; using Yi.Framework.Model.Models; using Yi.Framework.Repository; namespace Yi.Framework.Service { + [AppService] public class BaseService:IBaseService where T:class,new() { public IRepository _repository { get; set; } diff --git a/Yi.Framework.Net6/Yi.Framework.Task/HttpJob.cs b/Yi.Framework.Net6/Yi.Framework.Task/HttpJob.cs index bb16da9e..9e7c102c 100644 --- a/Yi.Framework.Net6/Yi.Framework.Task/HttpJob.cs +++ b/Yi.Framework.Net6/Yi.Framework.Task/HttpJob.cs @@ -19,7 +19,7 @@ namespace Yi.Framework.Job public Task Execute(IJobExecutionContext context) { - return Task.Run(() => + return Task.Run(async () => { var jobData = context.JobDetail.JobDataMap; string method= jobData[Common.Const.JobConst.method].ToString(); @@ -28,10 +28,10 @@ namespace Yi.Framework.Job switch (method) { case "post": - data = Common.Helper.HttpHelper.HttpPost(url); + data =await Common.Helper.HttpHelper.Post(url); break; case "get": - data = Common.Helper.HttpHelper.HttpGet(url); + data =await Common.Helper.HttpHelper.Get(url); break; } diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/AttributeExtend/GlobalLogAttribute.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/AttributeExtend/GlobalLogAttribute.cs index 68f30b49..4b02390e 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/AttributeExtend/GlobalLogAttribute.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/AttributeExtend/GlobalLogAttribute.cs @@ -8,6 +8,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Yi.Framework.Common.Attribute; using Yi.Framework.Common.Helper; using Yi.Framework.Common.Models; using Yi.Framework.Interface; diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/AutoIocExtend.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/AutoIocExtend.cs new file mode 100644 index 00000000..2396c09d --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/AutoIocExtend.cs @@ -0,0 +1,97 @@ +锘 +using Autofac; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Common.Attribute; + +namespace Yi.Framework.WebCore.MiddlewareExtend +{ + public static class AutoIocExtend + { + private static void RegIoc(ContainerBuilder build, Assembly assembly) + { + foreach (var type in assembly.GetTypes()) + { + + var serviceAttribute = type.GetCustomAttribute(); + if (serviceAttribute is not null) + { + //鎯呭喌1锛氫娇鐢ㄨ嚜瀹氫箟[AppService(ServiceType = typeof(娉ㄥ唽鎶借薄鎴栬呮帴鍙))]锛屾墜鍔ㄥ幓娉ㄥ唽锛屾斁type鍗冲彲 + var serviceType = serviceAttribute.ServiceType; + //鎯呭喌2 鑷姩鍘绘壘鎺ュ彛锛屽鏋滃瓨鍦ㄥ氨鏄帴鍙o紝濡傛灉涓嶅瓨鍦ㄥ氨鏄湰韬 + if (serviceType == null) + { + //鑾峰彇鏈闈犺繎鐨勬帴鍙 + var firstInter = type.GetInterfaces().LastOrDefault(); + if (firstInter is null) + { + serviceType = type; + } + else + { + serviceType = firstInter; + } + } + + switch (serviceAttribute.ServiceLifetime) + { + case LifeTime.Singleton: + if (type.IsGenericType) + { + build.RegisterGeneric(type).As(serviceType).SingleInstance(); + } + else + { + build.RegisterType(type).As(serviceType).SingleInstance(); + } + break; + case LifeTime.Scoped: + if (type.IsGenericType) + { + build.RegisterGeneric(type).As(serviceType).InstancePerLifetimeScope(); + } + else + { + build.RegisterType(type).As(serviceType).InstancePerLifetimeScope(); + } + break; + case LifeTime.Transient: + if (type.IsGenericType) + { + build.RegisterGeneric(type).As(serviceType).InstancePerDependency(); + } + else + { + build.RegisterType(type).As(serviceType).InstancePerDependency(); + } + break; + default: + if (type.IsGenericType) + { + build.RegisterGeneric(type).As(serviceType).InstancePerDependency(); + } + else + { + build.RegisterType(type).As(serviceType).InstancePerDependency(); + } + break; + } + } + } + } + + public static void AddAutoIocService(this ContainerBuilder build, params string[] assemblyStr) + { + foreach (var a in assemblyStr) + { + RegIoc(build, Assembly.Load(a)); + } + } + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/Utility/CustomAutofacModule.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/Utility/CustomAutofacModule.cs index e83a0a99..847c69b4 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/Utility/CustomAutofacModule.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/Utility/CustomAutofacModule.cs @@ -41,8 +41,8 @@ namespace Yi.Framework.WebCore.Utility containerBuilder.RegisterType< HttpContextAccessor>().As().SingleInstance(); - containerBuilder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope(); - containerBuilder.RegisterGeneric(typeof(BaseService<>)).As(typeof(IBaseService<>)).InstancePerLifetimeScope(); + //containerBuilder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope(); + //containerBuilder.RegisterGeneric(typeof(BaseService<>)).As(typeof(IBaseService<>)).InstancePerLifetimeScope(); ///鍙嶅皠娉ㄥ叆鏈嶅姟灞傚強鎺ュ彛灞 var assemblysServices = GetDll( "Yi.Framework.Service.dll"); containerBuilder.RegisterAssemblyTypes(assemblysServices) @@ -60,6 +60,9 @@ namespace Yi.Framework.WebCore.Utility containerBuilder.Register(c => new CustomAutofacAop());//AOP娉ㄥ唽 + + + //containerBuilder.RegisterType().As().EnableInterfaceInterceptors();寮鍚疉op //灏嗘暟鎹簱瀵硅薄娉ㄥ叆