@@ -55,23 +55,23 @@ private IBindingWithOrOnSyntax<T> ConfigureImplementationAndLifecycle<T>(
5555 ServiceDescriptor descriptor ,
5656 BindingIndex bindingIndex ) where T : class
5757 {
58- IBindingNamedWithOrOnSyntax < T > result ;
58+ IDescriptorAdapter adapter ;
5959#if NET8_0_OR_GREATER
6060 if ( descriptor . IsKeyedService )
6161 {
62- result = ConfigureImplementationAndLifecycleKeyed ( bindingToSyntax , descriptor ) ;
62+ adapter = new KeyedDescriptorAdapter ( descriptor ) ;
6363 }
6464#endif
6565#if NET8_0_OR_GREATER
6666 else
6767 {
6868#endif
69- result = ConfigureImplementationAndLifecycleNonKeyed ( bindingToSyntax , descriptor ) ;
69+ adapter = new DefaultDescriptorAdapter ( descriptor ) ;
7070#if NET8_0_OR_GREATER
7171 }
7272#endif
7373
74- var resultWithMetadata = result
74+ var resultWithMetadata = ConfigureImplementationAndLifecycleWithAdapter ( bindingToSyntax , adapter )
7575 . WithMetadata ( nameof ( ServiceDescriptor ) , descriptor )
7676 . WithMetadata ( nameof ( BindingIndex ) , bindingIndex . Next ( descriptor . ServiceType ) ) ;
7777
@@ -84,15 +84,15 @@ private IBindingWithOrOnSyntax<T> ConfigureImplementationAndLifecycle<T>(
8484 return resultWithMetadata ;
8585 }
8686
87- private IBindingNamedWithOrOnSyntax < T > ConfigureImplementationAndLifecycleNonKeyed < T > ( IBindingToSyntax < T > bindingToSyntax ,
88- ServiceDescriptor descriptor ) where T : class
87+ private IBindingNamedWithOrOnSyntax < T > ConfigureImplementationAndLifecycleWithAdapter < T > ( IBindingToSyntax < T > bindingToSyntax ,
88+ IDescriptorAdapter adapter ) where T : class
8989 {
9090 IBindingNamedWithOrOnSyntax < T > result ;
91- if ( descriptor . ImplementationType != null )
91+ if ( adapter . ImplementationType != null )
9292 {
93- result = ConfigureLifecycle ( bindingToSyntax . To ( descriptor . ImplementationType ) , descriptor . Lifetime ) ;
93+ result = ConfigureLifecycle ( bindingToSyntax . To ( adapter . ImplementationType ) , adapter . Lifetime ) ;
9494 }
95- else if ( descriptor . ImplementationFactory != null )
95+ else if ( adapter . UseServiceFactory )
9696 {
9797
9898 result = ConfigureLifecycle ( bindingToSyntax . ToMethod ( context
@@ -102,50 +102,18 @@ private IBindingNamedWithOrOnSyntax<T> ConfigureImplementationAndLifecycleNonKey
102102 // correct _scoped_ IServiceProvider is used. Fall back to root IServiceProvider when not created
103103 // through a NinjectServiceProvider (some tests do this to prove a point)
104104 var scopeProvider = context . GetServiceProviderScopeParameter ( ) ? . SourceServiceProvider ?? context . Kernel . Get < IServiceProvider > ( ) ;
105- return descriptor . ImplementationFactory ( scopeProvider ) as T ;
106- } ) , descriptor . Lifetime ) ;
105+ return adapter . InstantiateFromServiceFactory ( scopeProvider ) as T ;
106+ } ) , adapter . Lifetime ) ;
107107 }
108108 else
109109 {
110110 // use ToMethod here as ToConstant has the wrong return type.
111- result = bindingToSyntax . ToMethod ( context => descriptor . ImplementationInstance as T ) . InSingletonScope ( ) ;
111+ result = bindingToSyntax . ToMethod ( context => adapter . ImplementationInstance as T ) . InSingletonScope ( ) ;
112112 }
113113
114114 return result ;
115115 }
116116
117- #if NET8_0_OR_GREATER
118- private IBindingNamedWithOrOnSyntax < T > ConfigureImplementationAndLifecycleKeyed < T > ( IBindingToSyntax < T > bindingToSyntax ,
119- ServiceDescriptor descriptor ) where T : class
120- {
121- IBindingNamedWithOrOnSyntax < T > result ;
122- if ( descriptor . KeyedImplementationType != null )
123- {
124- result = ConfigureLifecycle ( bindingToSyntax . To ( descriptor . KeyedImplementationType ) , descriptor . Lifetime ) ;
125- }
126- else if ( descriptor . KeyedImplementationFactory != null )
127- {
128-
129- result = ConfigureLifecycle ( bindingToSyntax . ToMethod ( context
130- =>
131- {
132- // When resolved through the ServiceProviderScopeResolutionRoot which adds this parameter, the
133- // correct _scoped_ IServiceProvider is used. Fall back to root IServiceProvider when not created
134- // through a NinjectServiceProvider (some tests do this to prove a point)
135- var scopeProvider = context . GetServiceProviderScopeParameter ( ) ? . SourceServiceProvider ?? context . Kernel . Get < IServiceProvider > ( ) ;
136- return descriptor . KeyedImplementationFactory ( scopeProvider , descriptor . ServiceKey ) as T ;
137- } ) , descriptor . Lifetime ) ;
138- }
139- else
140- {
141- // use ToMethod here as ToConstant has the wrong return type.
142- result = bindingToSyntax . ToMethod ( context => descriptor . KeyedImplementationInstance as T ) . InSingletonScope ( ) ;
143- }
144-
145- return result ;
146- }
147- #endif
148-
149117 private IBindingNamedWithOrOnSyntax < T > ConfigureLifecycle < T > (
150118 IBindingInSyntax < T > bindingInSyntax ,
151119 ServiceLifetime lifecycleKind )
0 commit comments