I've watched quite a few IOC/DI/ninject tutorials and videos over the last couple of days but I am still not convinced I get the point.
In most the examples they usually say something like, what happens if we want a Sword or a Shuriken, we need to define IWeapon. we want to seperate the knowledge of the actual weapon from the warrior.
So we inject the required IWeapon into the Warrior and then let Ninject (or other) get us the required class to go into the IWeapon (say sword or shuriken), but then THEY go ahead and create a default binding which create ONE SINGLE binding of a Sword to IWeapon.
How do we tell it which one to use? we cannot used named bindings because you cannot set the named binding on the weapon and then Get.
In my case I have a message I am reading from a queue, it will include the required details about what to send and who to send to.
I also have an interface which knows how to send a message with implementations for SMS, email, iphone etc. I am not able to understand how to use the DI in this case without having to put a switch somewhere in my code :-(
public interface INotify
{
void Send(Message msg);
}
public class Message
{
public Message(INotify notify)
{
_notify = notify;
}
public void Send()
{
_notify.Send(this);
}
readonly INotify _notify;
public string Type { get; set; }
public string Text{ get; set; }
public string Name { get; set; }
public string Number { get; set; }
public string Email { get; set; }
}
_kernel.Bind<INotify>().To<NotifyEmail>();
//kernel.Bind<INotify>().To<NotifySMS>().Named("sms");
//kernel.Bind<INotify>().To<NotifyAPNS>().Named("iphone");
var msg = _kernel.Get<Message>();
msg.Send();
Isn't the whole point to make it easy to instantiate the required class?