2013-01-20 29 views
1

考虑:如何将RegisterType用于实现多个接口的类?

public interface IFeedOperations : IOperationsWithPreInstalledData 
{ 
    ... 
} 

public class FeedOperations : IFeedOperations 
{ 
} 

如何使用RegisterType为实现IFeedOperations因此IOperationsWithPreInstalledData还有一类?

public class FeedsInstaller : IDependencyInstaller 
{ 
    public void Install(IDependencyContainer container) 
    { 
     container.RegisterType(typeof(IFeedOperations), typeof(FeedOperations)); 
     container.RegisterType(typeof(IOperationsWithPreInstalledData), typeof(FeedOperations)); 
    } 
} 

当前的代码产生以下错误。如果我删除第二个RegisterType,那么当我拨打container.ResolveAll<IOperationsWithPreInstalledData>()时,我将得不到结果。

Component ... Feeds.FeedOperations不能被 注册。已经有一个名称的组件。您是否想要 修改现有组件?如果没有,请确保您 指定了一个唯一的名称。

如果我删除第二个RegisterType,那么当我拨打container.ResolveAll<IOperationsWithPreInstalledData>()时,我将得不到结果。温莎城堡似乎没有看到实施IFeedOperations的课程也实施了IOperationsWithPreInstalledData

如何在Castle Windsor注册我的实现类,以便它知道我的类实现了两个接口 - 或者说我的类可以解决任一接口。

+0

您是否在意创建两个类的实例(每个接口一个)? – svick

+0

宁愿只举一个例子,但在这种情况下我不挑剔。 – kingdango

+0

另外,'IDependencyContainer'似乎不是来自Castle的任何类型。这是您的自定义界面吗? – svick

回答

5
container.Register(Component.For<IFoo,IBar>().ImplementedBy<FooBar>()); 
+0

是的,这是我所做的,让它工作 - 计算它出于我自己,但很高兴给你的答案的功劳。谢谢你的回应! – kingdango

+0

是否与编写'For ().Forward ()'? –