Asp.Net Core 扩展IOC注入Attribute

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

    public static class ServiceInjectSetup
    {
        public static void AddServiceInjectTransientSetup(this IServiceCollection services, System.Reflection.Assembly assembly)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            foreach (Type item in assembly.GetTypes())
            {
                Transient attribute = item.GetCustomAttribute(typeof(Transient)) as Transient;
                if (attribute != null)
                {
                    services.AddTransient(attribute.ServiceType, item);
                }
            }
        }

        public static void AddServiceInjectScopedSetup(this IServiceCollection services, System.Reflection.Assembly assembly)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            foreach (Type item in assembly.GetTypes())
            {
                Scoped attribute = item.GetCustomAttribute(typeof(Scoped)) as Scoped;
                if (attribute != null)
                {
                    services.AddScoped(attribute.ServiceType, item);
                }
            }
        }

        public static void AddServiceInjectSingletonSetup(this IServiceCollection services, System.Reflection.Assembly assembly)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            foreach (Type item in assembly.GetTypes())
            {
                Singleton attribute = item.GetCustomAttribute(typeof(Singleton)) as Singleton;
                if (attribute != null)
                {
                    services.AddSingleton(attribute.ServiceType, item);
                }
            }
        }
    }

自定义属性

    public class Transient : Attribute
    {
        public Type ServiceType { get; set; }

        public Transient(Type serviceType)
        {
            ServiceType = serviceType;
        }
    }

    public class Singleton : Attribute
    {
        public Type ServiceType { get; set; }

        public Singleton(Type serviceType)
        {
            ServiceType = serviceType;
        }
    }

    public class Scoped : Attribute
    {
        public Type ServiceType { get; set; }

        public Scoped(Type serviceType)
        {
            ServiceType = serviceType;
        }
    }

再startup类调用服务

  services.AddServiceInjectScopedSetup(this.GetType().Assembly);
  services.AddServiceInjectTransientSetup(this.GetType().Assembly);
  services.AddServiceInjectSingletonSetup(this.GetType().Assembly);

标注服务

    [Scoped(serviceType: typeof(ItestRepository))]
    public class TestServices : ItestRepository
    {
        public string test()
        {
            return "张三";
        }
    }

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

posted @ 2020-06-21 21:36  Blog老中医  阅读(926)  评论(0编辑  收藏  举报