11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4-
4+ using System . Reflection ;
55using Microsoft . Extensions . DependencyInjection ;
6-
76using Unity . Injection ;
8- using Unity . Lifetime ;
97
108namespace Unity . Microsoft . DependencyInjection
119{
12- public static class Configuration
10+ internal static class Configuration
1311 {
14- static Aggregates _aggregates ;
12+ static List < Aggregate > _aggregates ;
1513
16- public static IServiceProvider Configure ( this IUnityContainer container , IServiceCollection services )
14+ internal static void Configure ( this IUnityContainer container , IServiceCollection services )
1715 {
18- container . AddNewExtension < MDIExtension > ( ) ;
19-
20- var provider = new ServiceProvider ( container ) ;
21-
2216 var aggregateTypes = GetAggregateTypes ( services ) ;
2317
24- var aggregateList = aggregateTypes . Select ( t => new Aggregate ( t , container ) ) . ToList ( ) ;
25- _aggregates = new Aggregates ( aggregateList ) ;
18+ _aggregates = aggregateTypes . Select ( t => new Aggregate ( t , container ) ) . ToList ( ) ;
2619 container . RegisterInstance ( _aggregates ) ;
2720
2821 // Configure all registrations into Unity
2922 foreach ( var serviceDescriptor in services )
3023 {
3124 container . RegisterType ( serviceDescriptor , _aggregates ) ;
3225 }
33- _aggregates . Register ( ) ;
3426
35- container . RegisterInstance < IServiceScopeFactory > ( provider ) ;
36- container . RegisterType < TransientObjectPool > ( new HierarchicalLifetimeManager ( ) ) ;
37-
38- return provider ;
27+ foreach ( var type in _aggregates )
28+ {
29+ type . Register ( ) ;
30+ }
3931 }
4032
4133 internal static void Register ( this IUnityContainer container ,
@@ -61,26 +53,20 @@ internal static void Register(this IUnityContainer container,
6153
6254 private static HashSet < Type > GetAggregateTypes ( IServiceCollection services )
6355 {
64- var aggregateTypes = new HashSet < Type >
65- (
66- services .
67- GroupBy
68- (
69- serviceDescriptor => serviceDescriptor . ServiceType ,
70- serviceDescriptor => serviceDescriptor
71- ) .
72- Where ( typeGrouping => typeGrouping . Count ( ) > 1 ) .
73- Select ( type => type . Key )
74- ) ;
75- return aggregateTypes ;
56+ var enumerable = services . GroupBy ( serviceDescriptor => serviceDescriptor . ServiceType ,
57+ serviceDescriptor => serviceDescriptor )
58+ . Where ( typeGrouping => typeGrouping . Count ( ) > 1 )
59+ . Select ( type => type . Key ) ;
60+
61+ return new HashSet < Type > ( enumerable ) ;
7662 }
7763
7864
7965
8066 private static void RegisterType ( this IUnityContainer container ,
81- ServiceDescriptor serviceDescriptor , Aggregates aggregates )
67+ ServiceDescriptor serviceDescriptor , List < Aggregate > aggregates )
8268 {
83- var aggregate = aggregates . Get ( serviceDescriptor . ServiceType ) ;
69+ var aggregate = aggregates . FirstOrDefault ( a => a . Type == serviceDescriptor . ServiceType ) ;
8470 if ( aggregate != null )
8571 aggregate . AddService ( serviceDescriptor ) ;
8672 else
@@ -121,17 +107,19 @@ private static void RegisterSingleton(this IUnityContainer container,
121107
122108 internal static bool CanResolve ( this IUnityContainer container , Type type )
123109 {
124- if ( type . IsClass && ! type . IsAbstract )
110+ var info = type . GetTypeInfo ( ) ;
111+
112+ if ( info . IsClass && ! info . IsAbstract )
125113 {
126- if ( typeof ( Delegate ) . IsAssignableFrom ( type ) || typeof ( string ) == type || type . IsEnum
127- || type . IsArray || type . IsPrimitive )
114+ if ( typeof ( Delegate ) . GetTypeInfo ( ) . IsAssignableFrom ( info ) || typeof ( string ) == type || info . IsEnum
115+ || type . IsArray || info . IsPrimitive )
128116 {
129117 return container . IsRegistered ( type ) ;
130118 }
131119 return true ;
132120 }
133121
134- if ( type . IsGenericType )
122+ if ( info . IsGenericType )
135123 {
136124 var gerericType = type . GetGenericTypeDefinition ( ) ;
137125 if ( ( gerericType == typeof ( IEnumerable < > ) ) ||
0 commit comments