Castle Windsor:如何使用一个实现实例注册两个服务?
如何用一个实现实例注册两个服务?我使用:
_container.Register(Component.For(new [] { typeof(IHomeViewModel), typeof(IPageViewModel) }).
ImplementedBy(typeof(HomeViewModel)).Named("IHomeViewModel").LifeStyle.Singleton)
但是上面的代码注册了两个 HomeViewModel 实例。
How to register two services with one instance of implementation? I used:
_container.Register(Component.For(new [] { typeof(IHomeViewModel), typeof(IPageViewModel) }).
ImplementedBy(typeof(HomeViewModel)).Named("IHomeViewModel").LifeStyle.Singleton)
But upper code registers two instances of HomeViewModel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这正是这样做的方法。请参阅文档中的“类型转发”。它注册一个可通过 IHomeViewModel 或 IPageViewModel 访问的逻辑组件。以下测试通过:
顺便说一句,您可能想使用泛型而不是所有那些
typeof
,并且还删除生活方式声明,因为单例是默认值:That's exactly the way to do it. See "Type Forwarding" in the docs. It registers one logical component accessible via IHomeViewModel or IPageViewModel. The following test passes:
BTW you might want to use generics instead of all those
typeof
, and also remove the lifestyle declaration, since singleton is the default: