troubleshooting Question

IOC Container - Composition Root

Avatar of chudmarek
chudmarek asked on
4 Comments1 Solution102 ViewsLast Modified:
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
Aaron Jabamani
Technical Architect

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 4 Comments.
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>


Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 4 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004