ASP.NET CORE study of native DI achieve mass registration

Original: Native DI ASP.NET CORE learning to achieve mass registration

Previously used Autofac time, just a AsImplementInterfaces () can be very easily batch registration function. The asp.net core built-in DI framework is no readily available bulk registration method, taking into account the somewhat cumbersome process to replace Autofac framework, then write their own extensions to achieve a simple native DI bulk registration features

Startup.cs extension

. 1  public  static  class StartUpExtenions
 2      {
 . 3          ///  <Summary> 
. 4          /// batch registration service
 . 5          ///  </ Summary> 
. 6          ///  <param name = "Services"> the DI service </ param> 
. 7          // /  <param name = "assemblys"> require batch registration assembly set </ param> 
. 8          ///  <param name = "the baseType"> base class / interface </ param> 
. 9          ///  <param name = "serviceLifetime "> service lifecycle </ param> 
10          ///  <returns A> </ returns A>
11         public staticBatchRegisterService IServiceCollection ( the this IServiceCollection Services, Assembly [] assemblys, the baseType the Type, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton)
 12 is          {
 13 is              List <the Type> = typelist new new List <the Type> ();   // all meet the registration conditions set of classes 
14              the foreach ( var assembly in assemblys)
 15              {
 16                  // the current filter assembly qualified class 
. 17                  var types assembly.GetTypes = (). the Where (T =>! t.IsInterface &&! t.IsSealed &&! && t.IsAbstract baseType.IsAssignableFrom (T));
 18 is                  IF (types != null && types.Count() > 0)
19                     typeList.AddRange(types);
20             }
21             if (typeList.Count() == 0)
22                 return services;
23 
24             var typeDic = new Dictionary<Type, Type[]>(); //待注册集合
25             foreach (var type in typeList)
26             {
27                 var interfaces = type.GetInterfaces();   //获取接口
28                 typeDic.Add (type, the interfaces);
 29              }
 30              IF (typeDic.Keys.Count ()> 0 )
 31 is              {
 32                  the foreach ( var instanceType in typeDic.Keys)
 33 is                  {
 34 is                      the foreach ( var InterfaceType in typeDic [instanceType])
 35                      {
 36                          // register according to the specified life cycle of 
37 [                          Switch (serviceLifetime)
 38 is                          {
 39                              Case ServiceLifetime.Scoped:
40                                 services.AddScoped(interfaceType, instanceType);
41                                 break;
42                             case ServiceLifetime.Singleton:
43                                 services.AddSingleton(interfaceType, instanceType);
44                                 break;
45                             case ServiceLifetime.Transient:
46                                 services.AddTransient(interfaceType, instanceType);
47                                 break;
48                         }
49                     }
50                 }
51             }
52             return services;
53         }
54     }

 

Call bulk registration process in ConfigureServices

 1 services.BatchRegisterService(new Assembly[] { Assembly.GetExecutingAssembly(), Assembly.Load("Test.DAL") }, typeof(IDependency)); 

 

Testing by the way, use the extended bulk registration of registered service class work

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/10965633.html