瀏覽代碼

新增NonRegisterIOCAttribute特性,在服务、仓储或其它类上新增[NonRegisterIOC]特性则不注册到第三方IOC容器
新增IRegisterIOC接口,在不以Service或者Repository命名结尾的类上继承IRegisterIOC接口则注册到第三方IOC容器

zhontai 2 年之前
父節點
當前提交
d2bb884c4f

+ 11 - 0
src/platform/ZhonTai.Admin/Core/Attributes/NonRegisterIOCAttribute.cs

@@ -0,0 +1,11 @@
+using System;
+
+namespace ZhonTai.Admin.Core.Attributes;
+
+/// <summary>
+/// 不注册到第三方IOC容器
+/// </summary>
+[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
+public class NonRegisterIOCAttribute : Attribute
+{
+}

+ 2 - 5
src/platform/ZhonTai.Admin/Core/HostApp.cs

@@ -116,11 +116,8 @@ public class HostApp
             // 单例注入
             builder.RegisterModule(new SingleInstanceModule(appConfig));
 
-            // 仓储注入
-            builder.RegisterModule(new RepositoryModule(appConfig));
-
-            // 服务注入
-            builder.RegisterModule(new ServiceModule(appConfig));
+            // 模块注入
+            builder.RegisterModule(new RegisterModule(appConfig));
         });
 
         //配置Kestrel服务器

+ 9 - 0
src/platform/ZhonTai.Admin/Core/RegisterModules/IRegisterIOC.cs

@@ -0,0 +1,9 @@
+namespace ZhonTai.Admin.Core.RegisterModules;
+
+/// <summary>
+/// 注册到第三方IOC容器
+/// </summary>
+public interface IRegisterIOC
+{
+    
+}

+ 18 - 5
src/platform/ZhonTai.Admin/Core/RegisterModules/ServiceModule.cs → src/platform/ZhonTai.Admin/Core/RegisterModules/RegisterModule.cs

@@ -8,18 +8,20 @@ using Module = Autofac.Module;
 using Microsoft.Extensions.DependencyModel;
 using ZhonTai.Admin.Core.Configs;
 using ZhonTai.Admin.Core.Db.Transaction;
+using ZhonTai.Admin.Core.Attributes;
+using ZhonTai.Admin.Core.Repositories;
 
 namespace ZhonTai.Admin.Core.RegisterModules;
 
-public class ServiceModule : Module
+public class RegisterModule : Module
 {
     private readonly AppConfig _appConfig;
 
     /// <summary>
-    /// 服务注入
+    /// 模块注入
     /// </summary>
     /// <param name="appConfig">AppConfig</param>
-    public ServiceModule(AppConfig appConfig)
+    public RegisterModule(AppConfig appConfig)
     {
         _appConfig = appConfig;
     }
@@ -42,9 +44,16 @@ public class ServiceModule : Module
                 .Where(a => _appConfig.AssemblyNames.Contains(a.Name))
                 .Select(o => Assembly.Load(new AssemblyName(o.Name))).ToArray();
 
+            var nonRegisterIOCAttribute = typeof(NonRegisterIOCAttribute);
+            var iRegisterIOCType = typeof(IRegisterIOC);
+
+            bool Predicate(Type a) => !a.IsDefined(nonRegisterIOCAttribute, true) 
+                && (a.Name.EndsWith("Service") || a.Name.EndsWith("Repository") || iRegisterIOCType.IsAssignableFrom(a)) 
+                && !a.IsAbstract && !a.IsInterface && a.IsPublic;
+
             //服务接口实例
             builder.RegisterAssemblyTypes(assemblies)
-            .Where(a => a.Name.EndsWith("Service"))
+            .Where(Predicate)
             .AsImplementedInterfaces()
             .InstancePerLifetimeScope()
             .PropertiesAutowired()// 属性注入
@@ -53,11 +62,15 @@ public class ServiceModule : Module
 
             //服务实例
             builder.RegisterAssemblyTypes(assemblies)
-            .Where(a => a.Name.EndsWith("Service"))
+            .Where(Predicate)
             .InstancePerLifetimeScope()
             .PropertiesAutowired()// 属性注入
             .InterceptedBy(interceptorServiceTypes.ToArray())
             .EnableClassInterceptors();
+
+            //泛型注入
+            builder.RegisterGeneric(typeof(RepositoryBase<>)).As(typeof(IRepositoryBase<>)).InstancePerLifetimeScope().PropertiesAutowired();
+            builder.RegisterGeneric(typeof(RepositoryBase<,>)).As(typeof(IRepositoryBase<,>)).InstancePerLifetimeScope().PropertiesAutowired();
         }
     }
 }

+ 0 - 49
src/platform/ZhonTai.Admin/Core/RegisterModules/RepositoryModule.cs

@@ -1,49 +0,0 @@
-
-using System.Linq;
-using System.Reflection;
-using Autofac;
-using Module = Autofac.Module;
-using Microsoft.Extensions.DependencyModel;
-using ZhonTai.Admin.Core.Repositories;
-using ZhonTai.Admin.Core.Configs;
-
-namespace ZhonTai.Admin.Core.RegisterModules
-{
-    public class RepositoryModule : Module
-    {
-        private readonly AppConfig _appConfig;
-
-        /// <summary>
-        /// 仓储注入
-        /// </summary>
-        /// <param name="appConfig">AppConfig</param>
-        public RepositoryModule(AppConfig appConfig)
-        {
-            _appConfig = appConfig;
-        }
-
-        protected override void Load(ContainerBuilder builder)
-        {
-            
-
-            if(_appConfig.AssemblyNames?.Length > 0)
-            {
-                //仓储
-                Assembly[] assemblies = DependencyContext.Default.RuntimeLibraries
-                    .Where(a => _appConfig.AssemblyNames.Contains(a.Name))
-                    .Select(o => Assembly.Load(new AssemblyName(o.Name))).ToArray();
-
-                builder.RegisterAssemblyTypes(assemblies)
-                .Where(a => a.Name.EndsWith("Repository"))
-                .AsImplementedInterfaces()
-                .InstancePerLifetimeScope()
-                .PropertiesAutowired();// 属性注入
-            }
-            
-
-            //泛型注入
-            builder.RegisterGeneric(typeof(RepositoryBase<>)).As(typeof(IRepositoryBase<>)).InstancePerLifetimeScope().PropertiesAutowired();
-            builder.RegisterGeneric(typeof(RepositoryBase<,>)).As(typeof(IRepositoryBase<,>)).InstancePerLifetimeScope().PropertiesAutowired();
-        }
-    }
-}

+ 9 - 5
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -29,6 +29,11 @@
             不格式化结果数据
             </summary>
         </member>
+        <member name="T:ZhonTai.Admin.Core.Attributes.NonRegisterIOCAttribute">
+            <summary>
+            不注册到第三方IOC容器
+            </summary>
+        </member>
         <member name="T:ZhonTai.Admin.Core.Attributes.NoOprationLogAttribute">
             <summary>
             禁用操作日志
@@ -1744,15 +1749,14 @@
             控制器注入
             </summary>
         </member>
-        <member name="M:ZhonTai.Admin.Core.RegisterModules.RepositoryModule.#ctor(ZhonTai.Admin.Core.Configs.AppConfig)">
+        <member name="T:ZhonTai.Admin.Core.RegisterModules.IRegisterIOC">
             <summary>
-            仓储注入
+            注册到第三方IOC容器
             </summary>
-            <param name="appConfig">AppConfig</param>
         </member>
-        <member name="M:ZhonTai.Admin.Core.RegisterModules.ServiceModule.#ctor(ZhonTai.Admin.Core.Configs.AppConfig)">
+        <member name="M:ZhonTai.Admin.Core.RegisterModules.RegisterModule.#ctor(ZhonTai.Admin.Core.Configs.AppConfig)">
             <summary>
-            服务注入
+            模块注入
             </summary>
             <param name="appConfig">AppConfig</param>
         </member>