IoC - Multiple implementations support for a single interface

后端 未结 7 1522
清歌不尽
清歌不尽 2020-12-30 02:50

I am wondering why .Net IoC containers do not easily support multiple implementations for a single interface! May be I am wrong, but as far I have seen, frameworks like Nin

7条回答
  •  余生分开走
    2020-12-30 03:19

    I advice to look at Convention over Configuration and especially Convention Based Dependency Injection and context-based dependency injection. Most of IoC if not all supports both approaches. You could find many interesting samples with different IoC libraries, when several implementation bind to one interface, and how useful it could be.

    For example ninject does support binding of several implementation of one interface: depends on context or attributes, by names, and so one.

    By context

    Following snippet binds on implemetation depends on target type automaticaly:

    Bind().To().WhenInjectedInto(typeof(OnLandAttack));
    Bind().To().WhenInjectedInto(typeof(AmphibiousAttack));
    

    By name

    Very helpful when you configuration is in XML or database. Take into account InNamedScope also:

    Bind().To().Named("Strong");
    Bind().To().Named("Weak");
    

    By convention

    With different dependency configuration at different parts of your project.

提交回复
热议问题