问题描述
例如,我有两个接口:ICustomerService和IOrderService,每个接口都有几个函数,例如GetCustomer,GetOrder等。
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.
温莎城堡对此有何反应?
首先有可能吗?
当我基于两个接口之一解析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.
假定生活方式保留为默认值:Singleton。
assuming the LifeStyle is left to its default: Singleton.
推荐答案
在CLR类型和Windsor服务或组件之间()。
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.
这篇关于温莎城堡如何响应实现多个接口的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!