zoukankan      html  css  js  c++  java
  • Asp.Net Core 扩展IOC注入Attribute

    IOC批量注入再Core框架中还是比较麻烦的,因此写了一个简单的IOC注入通过属性标注服务,再通过service自带的注册服务,扩展了三个注入服务,分别为
    AddTransientList/AddScopedList/AddSingletonList 下面直接上代码

    ServiceInjectExtensions类

        public static class ServiceInjectExtensions
        {
            public static void AddTransientList(this IServiceCollection services, System.Reflection.Assembly assembly)
            {
                if (services == null)
                {
                    throw new ArgumentNullException(nameof(services));
                }
                foreach (Type item in assembly.GetTypes())
                {
                    if (item.GetCustomAttributes(typeof(Transient)).Count() > 0)
                    {
                        if (!item.IsInterface)
                        {
                            services.AddTransient(item.GetInterfaces()[0], item);
                        }
                    }
                }
            }
    
            public static void AddScopedList(this IServiceCollection services, System.Reflection.Assembly assembly)
            {
                if (services == null)
                {
                    throw new ArgumentNullException(nameof(services));
                }
                foreach (Type item in assembly.GetTypes())
                {
                    if (item.GetCustomAttributes(typeof(Scoped)).Count() > 0)
                    {
                        if (!item.IsInterface)
                        {
                            services.AddTransient(item.GetInterfaces()[0], item);
                        }
                    }
                }
            }
    
            public static void AddSingletonList(this IServiceCollection services, System.Reflection.Assembly assembly)
            {
                if (services == null)
                {
                    throw new ArgumentNullException(nameof(services));
                }
                foreach (Type item in assembly.GetTypes())
                {
                    if (item.GetCustomAttributes(typeof(Singleton)).Count() > 0)
                    {
                        if (!item.IsInterface)
                        {
                            services.AddTransient(item.GetInterfaces()[0], item);
                        }
                    }
                }
            }
        }
    

    自定义属性

      public class Transient : Attribute
        {
        }
    
        public class Singleton : Attribute
        {
        }
    
        public class Scoped : Attribute
        {
        }
    

    再startup类调用服务

      services.AddTransientList(Assembly.GetExecutingAssembly());
      services.AddScopedList(Assembly.GetExecutingAssembly());
      services.AddSingletonList(Assembly.GetExecutingAssembly());
    

    标注服务

        [ExtensionsInject.Transient()]
        public class TestServices : ItestRepository
        {
            public string test()
            {
                return "张三";
            }
        }
    

    再控制器中使用即可,有很多种注入方式,我比较喜欢的这种方式

  • 相关阅读:
    Asp.net相关知识和经验的碎片化记录
    JavaScript相关知识和经验的碎片化记录
    HTML相关知识和经验的碎片化记录
    CSS相关知识和经验的碎片化记录
    C# 实现文件(夹)在ftp服务器间的同步【无需将文件(夹)保存到本地】
    js中call、apply、bind的用法
    js中的数组对象排序
    CSS3图片轮播效果
    HTML5 canvas制作童年的回忆大风车
    js实现滑动器效果
  • 原文地址:https://www.cnblogs.com/SuperDust/p/13174134.html
Copyright © 2011-2022 走看看