I have seen this question Still need help understanding why Ninject might be better than manual DI but I am still confused by Ninject's usefulness...
I understand that this code...
class Samurai
{
readonly IWeapon weapon;
[Inject]
public Samurai(IWeapon weapon)
{
this.weapon = weapon;
}
public void Attack(string target)
{
this.weapon.Hit(target);
}
}
... Will produce the following "dynamic method that (basically) looks like this:"
delegate(IWeapon weapon)
{
return new Samurai(weapon);
}
How is this useful at all? Without Ninject, I can still do this (as per the Ninject docs "Dependency Injection By Hand" - No Ninject):
class Program
{
public static void Main()
{
var warrior1 = new Samurai(new Shuriken());
var warrior2 = new Samurai(new Sword());
warrior1.Attack("the evildoers");
warrior2.Attack("the evildoers");
}
}
What does using Ninject provide for me that I can't do by just following basic principals of loose coupling? Thanks for helping me understand.