简体   繁体   English

Windsor为多个接口注册单例组件

[英]Windsor register singleton component for multiple interfaces

I want to register one class with 2 interfaces in Castle.Windsor. 我想在Castle.Windsor中注册一个带有2个接口的类。

does this code work... Will I have only one instance for both interfaces... 这段代码是否有用...我是否只有两个接口的实例...

Component.For<IEnvironment>().ImplementedBy<OutlookEnvironment>().LifestyleSingleton()
Component.For<IOutlookEnvironment>().ImplementedBy<OutlookEnvironment>().LifestyleSingleton()

I need to double check this because my environment should always be the same instance... 我需要仔细检查这个,因为我的环境应该始终是同一个实例...

So when I resolve using the IEnvironment interface I should get the same instance as when using IOutlookEnvironment to resolve the component 因此,当我使用IEnvironment接口解析时,我应该获得与使用IOutlookEnvironment解析组件时相同的实例

You need to use the use multi-generic-parameter overload of the Component.For method 您需要使用Component.For方法的multi-generic-parameter重载

Component.For<IEnvironment, IOutlookEnvironment>()
         .ImplementedBy<OutlookEnvironment>()
         .LifestyleSingleton()

See also in the documentation: Registering component with multiple services section. 另请参阅文档: 使用多个服务注册组件部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM