Avatar of chudmarek

asked on 

IOC Container - Composition Root

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

Avatar of undefined
Last Comment
Frank Helk
Avatar of Aaron Jabamani
Aaron Jabamani
Flag of United Kingdom of Great Britain and Northern Ireland image


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;
Avatar of chudmarek


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.


Avatar of Aaron Jabamani
Aaron Jabamani
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Frank Helk
Frank Helk
Flag of Germany image

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

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo