We help IT Professionals succeed at work.

IOC Container - Composition Root

chudmarek asked
Last Modified: 2018-03-10
Hi Guys and Gals,

I've recently been dabbling a bit with IOC Containers (LightInject in my case).

I've been reading that you should only need to use the container ONCE, on startup, and no where else. This is what I'm struggling to understand. If I can only reference the container in a bootstrap/startup method, how is it possible to resolve what I need, elswhere in the project, or at runtime if the class depends on user input.

So In my Traditional Windows Forms App, on Form Load Say, I would Bootstrap Lightinject as per the attached file. It's only an arbitrary example, it's more the premise I need to get my head around.

I might be missing something here entirely, or just not getting it. But how am i supposed to resolve dependancies, If i can't use/not supposed to reference or use Container.GetInstance/Resolve/{Choose IOC Syntax Here}, and only in the composition root.

For Instance, Say I have two buttons and a TextBox on my form. The first button gets me an ILoader (attached file), and the second button loads up a file viewer (ILoader, attached file), whose file name is what is entered into the textbox.

Without An IOC Container I would do the following (let's just assume its put in the click event)

Button 1 would call ISplitText MyStringFunc =  new WhateverImplementsIt();

Button 2 (gets the file reader based on textbox input)

ILoader MyLoader = new FileReaderImplementation(TextBox1.Text);

Using LightInject, I'm surely compelled to do the following:

ISplitText Splitter = Container.GetInstance<ISplitText>();

Button 2
varLoaderFunc = Container.GetInstance<Func<string, ILoader>>();
ILoader l2 = LoaderFunc(TextBox1.Text);            

Am I Incorrect? In A large project I would have Container.GetInstance<IFoo>, peppered all over the place, in the main form file and elsewhere surely, so how can i only reference the container in ONLY 1 spot, in the form of  bootstrap, am i missing a magic piece of the puzzle?

In all the sample apps I have seen it's all done in one simple console app, in the Main function. All these apps follow the format of:

Container = new Container()

var Resolved = Container.GetInstance<IFoo>();

Well, I understand all that, and it's extremely simple. It's once you start adding a bit of complexity to the app itself, I'm lost as to how to get the instances without making the Container itself public, or static, or accessibly in some way,shape or form and then calling Container.GetInstance in a million places (which apparently, is a big no no). PLEASE HELP!


PS - I am not concerned about "abstracting the container" itself. so would prefer to only focus on my understanding of the above
Watch Question

Aaron JabamaniTechnical Architect


That is one way of getting the instance. But in general, people do constructor/property/interface  injection .  So you don't have to get the instance manually.  Below is a sample of constructor injection. Here any concrete class you register with ISpellchecker , will be assigned to checker automatically when TextEditor object is constructed.

public class TextEditor
    private ISpellChecker checker;
    public TextEditor(ISpellChecker checker)
        this.checker = checker;


Hi apeter,

That's fine. I understand all that, and this is how I design my classes, in the form of pure DI. However, where I am falling down and what I am missing is in the context of IOC and winforms.

How am i supposed to resolve the instance, based on the the user input, and specifically button click, If i Can't use Container.GetInstance<ILoader>(TextBox1.Text) ? I must be missing a piece of the puzzle.


Technical Architect
Unlock this solution and get a sample of our free trial.
(No credit card required)

No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: apeter (https:#a42093698)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Experts-Exchange Cleanup Volunteer

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.