1

Is it possible to resolve dependencies shown in this small program using only IoC container? How? How to register classes within a container so that each instance A form its own resolution scope?

Is it possible to use single Resolve-call to reproduce the program?

class A
{

}

class B
{
    private readonly A a;

    public B(A a)
    {
        this.a = a;
    }
}

class C
{
    private readonly B b;

    public C(B b)
    {
        this.b = b;
    }

    public void PrintHello()
    {
        Console.WriteLine("Hello StackOverflow!");
    }
}

class Program
{
    static void Main(string[] args)
    {
        for (var i = 0; i < 10; ++i)
        {
            var a = new A();
            var b = new B(a);
            var c = new C(b);

            c.PrintHello();
        }
    }
}
4
  • If you're asking for specific code to register your services you need to tell us which service container you're using, the syntax differs. Commented Jul 17, 2017 at 19:25
  • Yes it is possible using IoC container. Which container you prefer to use? Commented Jul 17, 2017 at 19:26
  • To be as specific as your question, most service containers support this, simply register all the services and the container will take care of the rest. Usually you don't need to do anything specific either, just make sure all the services are registered. Commented Jul 17, 2017 at 19:28
  • If the question is "which framework should I use", such question is off topic to Stackoverflow. Commented Jul 18, 2017 at 7:11

2 Answers 2

1

Depending on the IoC container used, the syntax and initialization would be different, but in any case it would be something like this:

Upon initialization:

var builder = new ContainerBuilder();
builder.RegisterType<A>().InstancePerDependency();
builder.RegisterType<B>().InstancePerDependency();
builder.RegisterType<C>().InstancePerDependency();

Then your program would simply be:

for (var i = 0; i < 10; ++i)
{
    var c = serviceProvider.getService<C>();
    c.PrintHello();
}

The IoC container will take care of providing new instances of B and A to the C and B ctors respectively.

Good luck!

Sign up to request clarification or add additional context in comments.

5 Comments

I think @Ckratide is right, but the question about inversion of control containers is quite broad since different containers out there have different ways of registering dependencies and resolving them but one fact remains, you have bind first before resolving for any instance. For nested dependencies, you have to use an advanced IoC container like Unity. Once you register A and B as in your case above, then try getting an instance of C using the container. It will automatically resolve the dependencies inside C.
@devTimmy - are there really popular containers that can't handle nesting? I haven't had any problems with this with Castle Windsor, for instance. Or Spring, over in Javaland.
The syntax I used is extracted from a .Net Core console program, using AutoFac as the IoC container.
Oh yeah, Unity is quite popular in .net and recently ASP.NET core released its version of an IoC container that ships with asp.net core.
How can we do this in default .net6.0 built in dependency injection.
0

This is example using Ninject container.

    static void Main(string[] args)
    {
        using (var kernel = new StandardKernel())
        {
            kernel.Bind<A>().To<A>();
            kernel.Bind<B>().To<B>();
            kernel.Bind<C>().To<C>();

            for (var i = 0; i < 10; ++i)
            {
                var c = kernel.Get<C>();
                c.PrintHello();
            }
        }
    }

The most popular containers provide nested dependency resolving capabilities. Compare them and choose your favourite.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.