Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

dependency injection - I'm confused about interface abstractions when using IoC

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A good IoC container should not change the way interfaces are used:

  1. An interface should be designed for the component that is using it as dependency and not the class that is implementing it. (interface segregation principle)
  2. A class can implement several interfaces. But this should only be done if those interfaces are for the same kind of service so that the class does exactly one thing. If the interfaces are for two different things they should be implemented by two different classes. (single responsibility principle)
  3. Several classes can implement the same interface if you need multiple strategies for this type of service.

Ninject allows using interfaces this way using two different concepts:

  1. Conditional bindings: If several classes implement the same interface you have to specify which implementation is used in which case. This is done using conditions:

    Bind<IWeapon>().To<Sword>().When(ctx => TodayIsSunday());

    Bind<IWeapon>().To<Dagger>().When(ctx => !TodayIsSunday());

  2. Multiple Interfaces: See my blogpost http://www.planetgeek.ch/2010/12/08/ninject-extension-contextpreservation-explained/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...