JasperFx / lamar

Fast Inversion of Control Tool and Successor to StructureMap
https://jasperfx.github.io/lamar
MIT License
568 stars 119 forks source link

Order of appended ServiceCollections should not matter #153

Closed e-tobi closed 5 years ago

e-tobi commented 5 years ago

This works (IWidget first):

var container = Container.Empty();
container.Configure(x => x.AddTransient<IWidget, AWidget>());
container.Configure(x => x.AddTransient<BaseThing, Thing1>());
container.GetInstance<BaseThing>().ShouldBeOfType<Thing1>();

This doesn't (BaseThing first):

var container = Container.Empty();
container.Configure(x => x.AddTransient<BaseThing, Thing1>());
container.Configure(x => x.AddTransient<IWidget, AWidget>());
container.GetInstance<BaseThing>().ShouldBeOfType<Thing1>();

I've encountered this when switching from SM to Lamar and got Issues with the Nancy bootstrapper. Nancy needs to register internal types which results in multiple .Configure() calls.

The WhatDoIHave():

Non-Working vase

=========================================================================================================================================
ServiceType              Namespace                                    Lifecycle     Description                      Name                
-----------------------------------------------------------------------------------------------------------------------------------------
BaseThing                StructureMap.Testing.Acceptance              Transient     new Thing1()                     thing1              
-----------------------------------------------------------------------------------------------------------------------------------------
IContainer               Lamar                                        Scoped        Current IContainer               IContainer          
-----------------------------------------------------------------------------------------------------------------------------------------
IServiceContext          Lamar                                        Scoped        Current IServiceContext          IServiceContext     
-----------------------------------------------------------------------------------------------------------------------------------------
IServiceProvider         System                                       Scoped        Current IServiceProvider         IServiceProvider    
-----------------------------------------------------------------------------------------------------------------------------------------
IServiceScopeFactory     Microsoft.Extensions.DependencyInjection     Singleton     Current IServiceScopeFactory     IServiceScopeFactory
-----------------------------------------------------------------------------------------------------------------------------------------
IWidget                  StructureMap.Testing.Widget                  Transient     new AWidget()                    aWidget             
-----------------------------------------------------------------------------------------------------------------------------------------
Scope                    Lamar.IoC                                    Scoped        Current Scope                    Scope               
=========================================================================================================================================

Working vase

=========================================================================================================================================
ServiceType              Namespace                                    Lifecycle     Description                      Name                
-----------------------------------------------------------------------------------------------------------------------------------------
BaseThing                StructureMap.Testing.Acceptance              Transient     new Thing1(widget)               thing1              
-----------------------------------------------------------------------------------------------------------------------------------------
IContainer               Lamar                                        Scoped        Current IContainer               IContainer          
-----------------------------------------------------------------------------------------------------------------------------------------
IServiceContext          Lamar                                        Scoped        Current IServiceContext          IServiceContext     
-----------------------------------------------------------------------------------------------------------------------------------------
IServiceProvider         System                                       Scoped        Current IServiceProvider         IServiceProvider    
-----------------------------------------------------------------------------------------------------------------------------------------
IServiceScopeFactory     Microsoft.Extensions.DependencyInjection     Singleton     Current IServiceScopeFactory     IServiceScopeFactory
-----------------------------------------------------------------------------------------------------------------------------------------
IWidget                  StructureMap.Testing.Widget                  Transient     new AWidget()                    aWidget             
-----------------------------------------------------------------------------------------------------------------------------------------
Scope                    Lamar.IoC                                    Scoped        Current Scope                    Scope               
=========================================================================================================================================
jeremydmiller commented 5 years ago

That behavior is absolutely necessary for ASP.Net Core compliance, so no dice. I'm afraid you're gonna have to take that up w/ the NancyFx guys