ASP.NET CORE 学习之原生DI实现批量注册
以前使用Autofac的时候,只需一句AsImplementInterfaces()就可以很轻松实现批量注册功能。而asp.net core内置的DI框架没有现成的批量注册方法,考虑到替换Autofac框架过程有些繁琐,于是自己写扩展实现了一个简易的原生DI批量注册功能
Startup.cs扩展
public static class StartUpExtenions { /// <summary> /// 批量注册服务 /// </summary> /// <param name="services">DI服务</param> /// <param name="assemblys">需要批量注册的程序集集合</param> /// <param name="baseType">基础类/接口</param> /// <param name="serviceLifetime">服务生命周期</param> /// <returns></returns> public static IServiceCollection BatchRegisterService(this IServiceCollection services, Assembly[] assemblys, Type baseType, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton) { List<Type> typeList = new List<Type>(); //所有符合注册条件的类集合 foreach (var assembly in assemblys) { //筛选当前程序集下符合条件的类 var types = assembly.GetTypes().Where(t => !t.IsInterface && !t.IsSealed && !t.IsAbstract && baseType.IsAssignableFrom(t)); if (types != null && types.Count() > 0) typeList.AddRange(types); } if (typeList.Count() == 0) return services; var typeDic = new Dictionary<Type, Type[]>(); //待注册集合 foreach (var type in typeList) { var interfaces = type.GetInterfaces(); //获取接口 typeDic.Add(type, interfaces); } if (typeDic.Keys.Count() > 0) { foreach (var instanceType in typeDic.Keys) { foreach (var interfaceType in typeDic[instanceType]) { //根据指定的生命周期进行注册 switch (serviceLifetime) { case ServiceLifetime.Scoped: services.AddScoped(interfaceType, instanceType); break; case ServiceLifetime.Singleton: services.AddSingleton(interfaceType, instanceType); break; case ServiceLifetime.Transient: services.AddTransient(interfaceType, instanceType); break; } } } } return services; } }