I read about Inversion of Control infrastructures, and I just play with the question: "Why the hell do I need the infrastructure for this?"
They didnโt understand my question ... the pattern is often used by programmers, but ... a fully functional structure for this?
I need to miss something and that I am asking a question. I have seen many examples on the Internet and I just do not understand. mi mind is locked for this idea.
Just take a look at the Ninject home page example:
public class Samurai { public IWeapon Weapon { get; private set; } public Samurai(IWeapon weapon) { Weapon = weapon; } } public class WarriorModule : NinjectModule { public override void Load() { Bind< IWeapon >.To< Sword >(); } }
The Samurai class suits me. The "NinjectModule" structure seems unnecessary to me.
I assume that later in the code we will create new instances of "samurai" passing in instances of the "Sword", for example:
Samurai theWarrior = new Samurai(WarriorModule.GetInstance(IWeapon));//no coupling
which can be replaced by:
Samurai theWarrior = new Samurai(new Sword());//still no coupling
or
Samurai theWarrior = new Samurai(GetWeaponFromXML());//no coupling yet
Which part am I missing? Could you tell us about some scenario where the Ioc structure may be needed in my application?
Thanks.
UPDATE AFTER 4 ANSWERS : I really liked all the answers I received from you guys. I just read this dependency-injection-dissection / post where the guy uses it to test Unit and the StackOverflow link you just provided, and yes, I missed -big-big complexity, so let me set myself up to use the IoC infrastructure. Thanks again.
I would vote for your answers, but I just get an orange message that I canโt.
Thanks to the guy who highlighted the code I posted.
ioc-container ninject
Rafael enriquez
source share