How does Castle Windsor respond to a class which implements multiple interfaces?

柔情痞子 提交于 2019-12-30 09:40:51

问题


For example I have two interfaces: ICustomerService and IOrderService which each has a couple of functions like GetCustomer, GetOrder, etc.

I want one class to implement both interfaces: Server.

How does Castle Windsor respond to this? Is it possible in the first place? When I resolve the Server object based on one of the two interfaces, will I get the same object? What happens when I have a constructor that has both interfaces in its parameters? Will there still be one object constructed.

assuming the LifeStyle is left to its default: Singleton.


回答1:


There's no hard one-to-one mapping between a CLR type and a Windsor service or component (glossary if needed is here).

So you can have any of the following scenarios:

  • Many Components with different implementation types expose a single Service

    container.Register(
       Component.For<IFoo>().ImplementedBy<Foo1>(),
       Component.For<IFoo>().ImplementedBy<Foo2>()
    );
    
  • Many Components with same implementation type expose a single Service

    container.Register(
       Component.For<IFoo>().ImplementedBy<Foo1>(),
       Component.For<IFoo>().ImplementedBy<Foo1>().Named("second")
    );
    
  • Many Components with same implementation type expose different Services

    container.Register(
       Component.For<IFoo>().ImplementedBy<Foo1>(),
       Component.For<IBar>().ImplementedBy<Foo1>().Named("second")
    );
    
  • Single Component expose different Services

    container.Register(
       Component.For<IFoo, Bar>().ImplementedBy<Foo1>()
    );
    

As you can see, yes - it is possible, and whether or not you'll get the same instance (assuming singleton) depends on which option you chose - if both Services will be exposed by the same Component, or not.




回答2:


Did you check this post?

Here also

Forum post also has info on this



来源:https://stackoverflow.com/questions/483515/how-does-castle-windsor-respond-to-a-class-which-implements-multiple-interfaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!