Hi, I've been reading about Inversion of Control frameworks and I'm just playing around with the question: "Why in the hell do I need a framework to do this?"
Don't misunderstood my question... the pattern is something we programmers use often but... a full featured framework to do that?
I must be missing something and that's the reason I'm posting the question. I've seen a lot of examples on the web and I just don't get it. mi mind is blocked to the idea maybe.
Just take a look at the Ninject Home page's 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 is ok to me. The "NinjectModule" framework seems unnecessary to me.
I'm assuming later in the code we will be creating new "Samurai" instances passing in "Sword" instances to it, something like:
Samurai theWarrior = new Samurai(WarriorModule.GetInstance(IWeapon));//no coupling
which could be replaced by:
Samurai theWarrior = new Samurai(new Sword());//still no coupling
or
Samurai theWarrior = new Samurai(GetWeaponFromXML());//no coupling yet
What's the part I'm missing? Could you please tell of some scenario where Ioc framework could be needed in my Application?
Thanks.
UPDATE AFTER 4 ANSWERS: I really liked all of the answers I got from you guys. I just read this post dependency-injection-dissection/ where the guy use it for Unit Testing and the StackOverflow link you just provided and Yeah, I was missing the big-big-big complexity part, so let custom myself to use a IoC framework. Thanks again.
I would vote your answers but I just get an orange message saying I can't.
Thanks to the guy who highlighted the code I posted.