JasperFx / lamar

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

Auto-resolving of concrete types fails when type is deeply nested #358

Closed seanterry closed 1 year ago

seanterry commented 1 year ago

Concrete types nested more than one level deep do not auto-resolve.

This one took me a bit to figure out what was happening and was due to an extra level of nesting I hadn't intended. I thought I should create an issue to at least document the case, though a workaround exists (don't nest this deep).

Repro:

using Lamar;

namespace Throwaway;

public class LamarConcreteTypeResolutionAssumptions
{
    public class Nested1
    {
        public class Nested2
        {
        }
    }

    // passes
    [Fact]
    public void resolves_nested1()
    {
        var container = new Container( _ => { } );
        var actual = container.GetInstance<Nested1>();
        Assert.NotNull( actual );
    }

    // fails
    [Fact]
    public void resolves_nested2()
    {
        var container = new Container( _ => { } );
        var actual = container.GetInstance<Nested1.Nested2>();

        /*
            Lamar.IoC.LamarMissingRegistrationException
            No service registrations exist or can be derived for Throwaway.LamarConcreteTypeResolutionAssumptions.Nested1.Nested2
               at Lamar.IoC.Scope.GetInstance(Type serviceType)
               at Lamar.IoC.Scope.GetInstance[T]()
               at Throwaway.LamarConcreteTypeResolutionAssumptions.resolves_nested2() 
         */

        Assert.NotNull( actual );
    }
}