.netcore自动依赖注入

参考(抄袭)了网上别人写的代码。

定义了3个空接口,需要依赖注入的类型,实现某一个接口则使用某一种注入方式。

// AddScoped 在这次请求中获取的对象都是同一个 
public interface IScoped
// AddSingleton 每次获取的时候都是同一个实例
public interface ISingleton
// AddTransient 每次从容器中获取的时候都是一个新的实例
public interface ITransient

扩展类

public static class ServiceCollectionExtension
    {
        /// <summary>
        /// 注入数据
        /// </summary>
        /// <param name="services"></param>
        public static IServiceCollection AddAutoDIService(this IServiceCollection services)
        {
            //var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
            string path = AppContext.BaseDirectory;
            var referencedAssemblies = Directory.GetFiles(path, "*.dll").Select(Assembly.LoadFrom).ToArray();
            Set(typeof(IScoped), path, referencedAssemblies, services);
            Set(typeof(ITransient), path, referencedAssemblies, services);
            Set(typeof(ISingleton), path, referencedAssemblies, services);
            return services;
        }

        private static void Set(Type iType, string path, Assembly[] referencedAssemblies, IServiceCollection services)
        {
            var types = referencedAssemblies
                .SelectMany(a => a.DefinedTypes)
                .Select(type => type.AsType())
                .Where(x => x != iType && iType.IsAssignableFrom(x)).ToArray();
            var implementTypes = types.Where(x => x.IsClass).ToArray();
            var interfaceTypes = types.Where(x => x.IsInterface).ToArray();
            foreach (var implementType in implementTypes)
            {
                var interfaceType = interfaceTypes.FirstOrDefault(x => x.IsAssignableFrom(implementType));
                if (interfaceType != null)
                {
                    if (iType.Name == nameof(IScoped))
                    {
                        services.AddScoped(interfaceType, implementType);
                    }
                    else if (iType.Name == nameof(ITransient))
                    {
                        services.AddTransient(interfaceType, implementType);
                    }
                    else if (iType.Name == nameof(ISingleton))
                    {
                        services.AddSingleton(interfaceType, implementType);
                    }
                }
                else
                {
                    if (iType.Name == nameof(IScoped))
                    {
                        services.AddScoped(implementType);
                    }
                    else if (iType.Name == nameof(ITransient))
                    {
                        services.AddTransient(implementType);
                    }
                    else if (iType.Name == nameof(ISingleton))
                    {
                        services.AddSingleton(implementType);
                    }
                }
            }
        }
    }
View Code

然后在 ConfigureServices 中调用

services.AddAutoDIService();

 

posted @ 2019-11-13 14:16  残梦天心  阅读(1739)  评论(0编辑  收藏  举报