Castle Windsor - multiple implementation of an interface

前端 未结 4 1550
小鲜肉
小鲜肉 2020-12-09 16:02

While registering components in Castle Windsor, how do we bind specific implementation of an interface to a component that has a dependency on that interface. I know in adva

4条回答
  •  被撕碎了的回忆
    2020-12-09 16:28

    I had a problem very like this, two implementation of one interface and two implementation of another interface. I wanted to force usage of particular implementations of those interfaces.

    My class structure looked like this -

    enter image description here

    I looked at the naming convention, but didn't really like it. Instead I used the following -

        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
        container.Register(
             Component.For().ImplementedBy()
            ,Component.For().ImplementedBy()
    
            ,Component.For().ImplementedBy()
                .DependsOn(Dependency.OnComponent())
    
            ,Component.For().ImplementedBy()
                .DependsOn(Dependency.OnComponent())
    
            ,Component.For().LifestyleTransient()
                .DependsOn(Dependency.OnComponent())
        );
    

    Full info about this approach is here. In the source code provided with that post I show two other ways of achieving the same result.

提交回复
热议问题