Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolving deeply (2+ levels) nested dependencies with dependencies in child container doesn't work as expected #4

Closed
davidarkemp opened this issue May 13, 2011 · 2 comments

Comments

@davidarkemp
Copy link

I have something like:

internal class Service2 : IService2
{
    public IService3 Service3 { get; private set; }

    public Service2(IService3 service3)
    {
        this.Service3 = service3;
    }
}

internal class Service3 : IService3
{
}

internal class Service4 : IService4
{
    public IService2 Service2 { get; private set; }

    public Service4(IService2 service1)
    {
        Service2 = service1;
    }
}

internal class Service5 : IService5
{
    public Service5(IService4 service4)
    {
        Service4 = service4;
    }

    public IService4 Service4 { get; private set; }
}

I register Service2, Service4 and Service5 in my main container:

container.Register<IService2, Service2>().AsMultiInstance();

container.Register<IService4, Service4>().AsMultiInstance();

container.Register<IService5, Service5>().AsMultiInstance();

Then I add an instance of Service3 (the innermost dependency) to a child container:

var child = container.GetChildContainer();

var nestedService = new Service3();
child.Register<IService3>(nestedService);

And finally try to resolve IService5 (the outermost interface) from the child container:

var service5 = child.Resolve<IService5>();

I get an exception with the following:

TinyIoC.TinyIoCResolutionException: Unable to resolve type: IService5 ---> 
    TinyIoC.TinyIoCResolutionException: Unable to resolve type: Service5 ---> 
        TinyIoC.TinyIoCResolutionException: Unable to resolve type: Service5

Having stepped through this with the debugger, it seems to change context from the child container to the parent container when trying to resolve IService4, then it no longer has access to the concrete instance of IService3.

davidarkemp pushed a commit to davidarkemp/TinyIoC that referenced this issue May 13, 2011
@davidarkemp
Copy link
Author

Added this as a failing test to: davidarkemp@90351c3

grumpydev added a commit that referenced this issue Aug 26, 2011
@grumpydev
Copy link
Owner

Sorry it's taken a while to get to this :-) Not sure if I've already fixed this (I know I did some tweaks to the nested container bits for Nancy), but I've just pulled in your updated test classes and test method and it passes fine in the latest version (or "Works On My Maching" anyway :))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants