如何在Castle Windsor的代码中设置数组/列表依赖项?

时间:2010-05-12 16:22:29

标签: dependency-injection castle-windsor castle

我有以下课程:

class Repository : IRepository
class ReadOnlyRepository : Repository

abstract class Command
abstract CommandImpl : Command
{
     public CommandImpl(Repository repository){}
}

class Service
{
    public Service (Command[] commands){}
}

我在代码中注册它们如下:

var container = new Container("WindsorCOntainer.config");
var container = new WindsorContainer(new XmlInterpreter("WindsorConfig.xml"));
container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel));
container.AddComponent("repository", typeof(RentServiceRepository));
container.Resolve<RentServiceRepository>();
container.AddComponent("command", typeof(COmmandImpl));
container.AddComponent("rentService", typeof (RentService));
container.Resolve<RentService>(); // Fails here

我收到“RentService正在等待依赖命令”的消息

我做错了什么?

谢谢,

1 个答案:

答案 0 :(得分:2)

您未将CommandImpl注册为Command,而是将其注册为CommandImpl

如果你这样做:

container.Register(Component.For<Command>().ImplementedBy<CommandImpl>());

它会起作用。

我建议您熟悉the documentation,尤其是installersregistration API