From 506686b11efbd874ad6d968f2462e054f7e50ea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Mon, 16 Jan 2023 20:55:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84autofac=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/AutoFacExtensions.cs | 7 +++- .../Extensions/AutoFacModuleExtensions.cs | 32 +++++++++++++++++++ .../Modules/AutoFacModuleEnum.cs | 13 ++++++++ .../Modules/PropertiesAutowiredModule.cs | 32 +++++++++++++++++++ .../Modules/YiAutoFacModule.cs | 15 +++++++++ .../Yi.Framework.Core.Autofac.csproj | 3 +- .../Attributes/AutowiredAttribute.cs | 13 ++++++++ .../Extensions/AutoIocAddExtensions.cs | 3 +- .../Yi.Framework.Core/Helper/ReflexHelper.cs | 2 +- .../Yi.Framework.Ddd.csproj | 1 + src/Yi.Framework/Yi.Framework.Web/Program.cs | 20 +++++++++--- .../Yi.Framework.Web/Test2Entity.cs | 11 +++++++ .../Yi.Framework.Web/TestEntity.cs | 1 + 13 files changed, 144 insertions(+), 9 deletions(-) create mode 100644 src/Yi.Framework/Yi.Framework.Core.Autofac/Extensions/AutoFacModuleExtensions.cs create mode 100644 src/Yi.Framework/Yi.Framework.Core.Autofac/Modules/AutoFacModuleEnum.cs create mode 100644 src/Yi.Framework/Yi.Framework.Core.Autofac/Modules/PropertiesAutowiredModule.cs create mode 100644 src/Yi.Framework/Yi.Framework.Core.Autofac/Modules/YiAutoFacModule.cs create mode 100644 src/Yi.Framework/Yi.Framework.Core/Attributes/AutowiredAttribute.cs create mode 100644 src/Yi.Framework/Yi.Framework.Web/Test2Entity.cs diff --git a/src/Yi.Framework/Yi.Framework.Core.Autofac/Extensions/AutoFacExtensions.cs b/src/Yi.Framework/Yi.Framework.Core.Autofac/Extensions/AutoFacExtensions.cs index 258314c1..1afba5bf 100644 --- a/src/Yi.Framework/Yi.Framework.Core.Autofac/Extensions/AutoFacExtensions.cs +++ b/src/Yi.Framework/Yi.Framework.Core.Autofac/Extensions/AutoFacExtensions.cs @@ -7,7 +7,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Yi.Framework.Autofac.Extensions +namespace Yi.Framework.Core.Autofac.Extensions { public static class AutoFacExtensions { @@ -18,5 +18,10 @@ namespace Yi.Framework.Autofac.Extensions return hostBuilder; } + public static IHostBuilder ConfigureAutoFacContainer(this IHostBuilder hostBuilder, Action configureDelegate) + { + hostBuilder.UseAutoFacServerProviderFactory(); + return hostBuilder.ConfigureContainer(configureDelegate); + } } } diff --git a/src/Yi.Framework/Yi.Framework.Core.Autofac/Extensions/AutoFacModuleExtensions.cs b/src/Yi.Framework/Yi.Framework.Core.Autofac/Extensions/AutoFacModuleExtensions.cs new file mode 100644 index 00000000..739f9d2e --- /dev/null +++ b/src/Yi.Framework/Yi.Framework.Core.Autofac/Extensions/AutoFacModuleExtensions.cs @@ -0,0 +1,32 @@ +using Autofac; +using Autofac.Core.Registration; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Core.Autofac.Modules; + +namespace Yi.Framework.Core.Autofac.Extensions +{ + public static class AutoFacModuleExtensions + { + public static void RegisterYiModule(this ContainerBuilder builder, params Assembly[] assemblies) where TModule : YiAutoFacModule, new() + { + new TModule().Load(builder, assemblies); + } + + + public static void RegisterYiModule(this ContainerBuilder builder, AutoFacModuleEnum autoFacModuleEnum, params Assembly[] assemblies) + { + switch (autoFacModuleEnum) + { + case AutoFacModuleEnum.PropertiesAutowiredModule: + new PropertiesAutowiredModule().Load(builder, assemblies); + break; + } + } + + } +} diff --git a/src/Yi.Framework/Yi.Framework.Core.Autofac/Modules/AutoFacModuleEnum.cs b/src/Yi.Framework/Yi.Framework.Core.Autofac/Modules/AutoFacModuleEnum.cs new file mode 100644 index 00000000..8b25c34b --- /dev/null +++ b/src/Yi.Framework/Yi.Framework.Core.Autofac/Modules/AutoFacModuleEnum.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Core.Autofac.Modules +{ + public enum AutoFacModuleEnum + { + PropertiesAutowiredModule + } +} diff --git a/src/Yi.Framework/Yi.Framework.Core.Autofac/Modules/PropertiesAutowiredModule.cs b/src/Yi.Framework/Yi.Framework.Core.Autofac/Modules/PropertiesAutowiredModule.cs new file mode 100644 index 00000000..289dd8c2 --- /dev/null +++ b/src/Yi.Framework/Yi.Framework.Core.Autofac/Modules/PropertiesAutowiredModule.cs @@ -0,0 +1,32 @@ +using Autofac; +using Autofac.Core; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Core.Attributes; + + +namespace Yi.Framework.Core.Autofac.Modules +{ + internal class PropertiesAutowiredModule : YiAutoFacModule + { + internal override void Load(ContainerBuilder containerBuilder, params Assembly[] assemblies) + { + containerBuilder.RegisterAssemblyTypes(assemblies) + .PropertiesAutowired(new AutowiredPropertySelector()); + } + + } + + public class AutowiredPropertySelector : IPropertySelector + { + public bool InjectProperty(PropertyInfo propertyInfo, object instance) + { + return propertyInfo.CustomAttributes.Any(it => it.AttributeType == typeof(AutowiredAttribute)); + } + } + +} diff --git a/src/Yi.Framework/Yi.Framework.Core.Autofac/Modules/YiAutoFacModule.cs b/src/Yi.Framework/Yi.Framework.Core.Autofac/Modules/YiAutoFacModule.cs new file mode 100644 index 00000000..68d60f6c --- /dev/null +++ b/src/Yi.Framework/Yi.Framework.Core.Autofac/Modules/YiAutoFacModule.cs @@ -0,0 +1,15 @@ +using Autofac; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Core.Autofac.Modules +{ + public abstract class YiAutoFacModule + { + internal abstract void Load(ContainerBuilder containerBuilder,params Assembly[] assemblies); + } +} diff --git a/src/Yi.Framework/Yi.Framework.Core.Autofac/Yi.Framework.Core.Autofac.csproj b/src/Yi.Framework/Yi.Framework.Core.Autofac/Yi.Framework.Core.Autofac.csproj index b8c46913..d30b69ea 100644 --- a/src/Yi.Framework/Yi.Framework.Core.Autofac/Yi.Framework.Core.Autofac.csproj +++ b/src/Yi.Framework/Yi.Framework.Core.Autofac/Yi.Framework.Core.Autofac.csproj @@ -12,7 +12,8 @@ - + + diff --git a/src/Yi.Framework/Yi.Framework.Core/Attributes/AutowiredAttribute.cs b/src/Yi.Framework/Yi.Framework.Core/Attributes/AutowiredAttribute.cs new file mode 100644 index 00000000..d9c752af --- /dev/null +++ b/src/Yi.Framework/Yi.Framework.Core/Attributes/AutowiredAttribute.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Core.Attributes +{ + [AttributeUsage(AttributeTargets.Property)] + public class AutowiredAttribute : Attribute + { + } +} diff --git a/src/Yi.Framework/Yi.Framework.Core/Extensions/AutoIocAddExtensions.cs b/src/Yi.Framework/Yi.Framework.Core/Extensions/AutoIocAddExtensions.cs index 53d11f0e..bc4d58f4 100644 --- a/src/Yi.Framework/Yi.Framework.Core/Extensions/AutoIocAddExtensions.cs +++ b/src/Yi.Framework/Yi.Framework.Core/Extensions/AutoIocAddExtensions.cs @@ -89,7 +89,8 @@ namespace Yi.Framework.Core.Extensions if (serviceInterfaces is not null) { var serviceType = type; - var firstInter = type.GetInterfaces().LastOrDefault(); + var firstInter = type.GetInterfaces().Where(u => u != typeof(ITransientDependency)).LastOrDefault(); + if (firstInter is not null) { serviceType = firstInter; diff --git a/src/Yi.Framework/Yi.Framework.Core/Helper/ReflexHelper.cs b/src/Yi.Framework/Yi.Framework.Core/Helper/ReflexHelper.cs index a2fba721..33316b1d 100644 --- a/src/Yi.Framework/Yi.Framework.Core/Helper/ReflexHelper.cs +++ b/src/Yi.Framework/Yi.Framework.Core/Helper/ReflexHelper.cs @@ -36,6 +36,7 @@ namespace Yi.Framework.Core.Helper return null; } + /// /// 设置对象属性值 /// @@ -48,7 +49,6 @@ namespace Yi.Framework.Core.Helper try { Type Ts = obj.GetType(); - //object v = Convert.ChangeType(Value, Ts.GetProperty(FieldName).PropertyType); Ts.GetProperty(FieldName).SetValue(obj, Value, null); return true; } diff --git a/src/Yi.Framework/Yi.Framework.Ddd.Application/Yi.Framework.Ddd.csproj b/src/Yi.Framework/Yi.Framework.Ddd.Application/Yi.Framework.Ddd.csproj index 0c576986..4d82bdc4 100644 --- a/src/Yi.Framework/Yi.Framework.Ddd.Application/Yi.Framework.Ddd.csproj +++ b/src/Yi.Framework/Yi.Framework.Ddd.Application/Yi.Framework.Ddd.csproj @@ -9,6 +9,7 @@ + diff --git a/src/Yi.Framework/Yi.Framework.Web/Program.cs b/src/Yi.Framework/Yi.Framework.Web/Program.cs index e86c30d0..c92b92d6 100644 --- a/src/Yi.Framework/Yi.Framework.Web/Program.cs +++ b/src/Yi.Framework/Yi.Framework.Web/Program.cs @@ -1,10 +1,12 @@ using AspNetCore.Microsoft.AspNetCore.Builder; +using Autofac; using System.Reflection; using Yi.Framework.Application; using Yi.Framework.Application.Contracts; -using Yi.Framework.Autofac.Extensions; using Yi.Framework.Core; +using Yi.Framework.Core.Autofac.Extensions; +using Yi.Framework.Core.Autofac.Modules; using Yi.Framework.Core.AutoMapper; using Yi.Framework.Core.Extensions; using Yi.Framework.Core.Sqlsugar; @@ -14,7 +16,7 @@ using Yi.Framework.Domain.Shared; using Yi.Framework.Sqlsugar; using Yi.Framework.Web; -//ÕâÀïÊÇ×îÔçÖ´Ðеĵط½ + var builder = WebApplication.CreateBuilder(args); builder.WebHost.UseUrls(builder.Configuration.GetValue("StartUrl")); @@ -32,12 +34,20 @@ builder.UseYiModules( typeof(YiFrameworkApplicationContractsModule).Assembly, typeof(YiFrameworkApplicationModule).Assembly, typeof(YiFrameworkWebModule).Assembly - + ); -//ʹÓÃautofac -builder.Host.UseAutoFacServerProviderFactory(); + +//Ìí¼ÓautofacÄ£¿é,ÐèÒªÌí¼ÓÄ£¿é +builder.Host.ConfigureAutoFacContainer(container => +{ + container.RegisterYiModule(AutoFacModuleEnum.PropertiesAutowiredModule, typeof(YiFrameworkWebModule).Assembly); +}); + var app = builder.Build(); + + +var t = app.Services.GetService(); app.MapControllers(); app.Run(); diff --git a/src/Yi.Framework/Yi.Framework.Web/Test2Entity.cs b/src/Yi.Framework/Yi.Framework.Web/Test2Entity.cs new file mode 100644 index 00000000..189fbd8b --- /dev/null +++ b/src/Yi.Framework/Yi.Framework.Web/Test2Entity.cs @@ -0,0 +1,11 @@ +using Yi.Framework.Core.Attributes; +using Yi.Framework.Core.DependencyInjection; + +namespace Yi.Framework.Web +{ + public class Test2Entity: ITransientDependency + { + [Autowired] + public TestEntity testEntity { get; set; } + } +} diff --git a/src/Yi.Framework/Yi.Framework.Web/TestEntity.cs b/src/Yi.Framework/Yi.Framework.Web/TestEntity.cs index b72749ed..21815202 100644 --- a/src/Yi.Framework/Yi.Framework.Web/TestEntity.cs +++ b/src/Yi.Framework/Yi.Framework.Web/TestEntity.cs @@ -4,5 +4,6 @@ namespace Yi.Framework.Web { public class TestEntity: ITransientDependency { + } }