I've recently been trying to learn IoC, and have a couple questions based on the following code:
public class WarriorModule : NinjectModule
{
public override void Load()
{
Bind<IWeapon>().To<Sword>();
Bind<Samurai>().ToSelf();
}
}
I'm having trouble grasping the new concept of interfaces. Before I would create an interface such as IRunnable
, implementing the function void Run()
. With IoC, I'm now viewing an interface as something that only maps to a single concrete class. Assuming that, how would I map multiple concrete classes to an interface? I keep reading that you can map multiple interfaces to a single concrete class, but not the other way around (unless this is where contextual mapping comes into play).
Assuming interfaces only map to a single object, when should I create an interface as opposed to having an object bind to itself? Either way you will have to change the same piece of code when a mapping changes correct?
Edit: I marked the answer I did because it helped me personally. Both comments are equally informative though.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…