Skip to content

indirect depencies are not resolved by the child kernel. #9

@Steinblock

Description

@Steinblock

First of all, I am not sure if this is an issue or by design If it is by design, I would appreciate a hint how to refactor my code.

Consider the following setup.

        public interface IService
        {
            string Name { get; }
        }
        public interface IServiceProvider
        {
            IService GetService();
        }
        public class Service : IService
        {
            public string Name { get; set; }
        }
        public class ServiceProvider : IServiceProvider
        {
            private IService service;
            public ServiceProvider(IService service)
            {
                this.service = service;
            }
            public IService GetService()
            {
                return service;
            }
        }

My binding looks like this:

var container = new StandardKernel();
container.Bind<IService>().ToMethod(ctx => new Service { Name = "Parent" });
container.Bind<IServiceProvider>().To<ServiceProvider>();
var provider1 = container.Get<IServiceProvider>();
var service1a = container.Get<IService>(); // = Parent
var service1b = provider1.GetService(); // = Parent

var child = new ChildKernel(container);
child.Bind<IService>().ToMethod(ctx => new Service { Name = "Child" });
var provider2 = child.Get<IServiceProvider>();
var service2a = child.Get<IService>(); // = Child
var service2b = provider2.GetService(); // = Parent

In the child kernel I want to bind IService to my custom implementation ("Child") which works but I also want IServiceProvider to use the custom implementation without rebinding (In fact I can't since in my real world app IServiceProvider is out of my scope).

I found two different entries for this:
a) issue #7 "Child kernel bindings not being used to resolve dependencies" which seems to be the same problem but is from 2013
b) the readme which claims Since version 3.0, implicit bindngs are resolved on the child kernel and not the parent kernel anymore.

b) sounds as this should be possible since 3.0. I am using Ninject 3.2 and Ninject.Extensions.ChildKernel 3.2` .

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions