这正是要做到这一点的方法。请参阅文档中的“Type Forwarding”。它注册一个可通过IHomeViewModel或IPageViewModel访问的逻辑组件。以下测试通过:
public interface IHomeViewModel {}
public interface IPageViewModel {}
public class HomeViewModel: IHomeViewModel, IPageViewModel {}
[Test]
public void Forward() {
var container = new WindsorContainer();
container.Register(Component.For(new[] {typeof (IHomeViewModel), typeof (IPageViewModel)})
.ImplementedBy(typeof(HomeViewModel)).Named("IHomeViewModel").LifeStyle.Singleton);
Assert.AreSame(container.Resolve<IHomeViewModel>(), container.Resolve<IPageViewModel>());
}
顺便说一句,你可能要改为使用泛型的所有typeof
,并且还去掉生活方式的声明,因为单是默认:
container.Register(Component.For<IHomeViewModel, IPageViewModel>()
.ImplementedBy<HomeViewModel>());
还,如果你不使用组件的名称(你只能通过类型解决它,并且不要在任何地方使用命名服务覆盖),那么也可以忽略该名称。 – 2010-09-30 21:47:33
谢谢,问题出在我的代码中。 – INs 2010-10-01 12:47:59