0

这让我对许多配置感到困惑。我一直在阅读文档,但我只是不明白。这是我的注册码:

ForRequestedType<SimpleWorkItemProcessor>().TheDefault.Is.OfConcreteType<SimpleWorkItemProcessor>();

ForRequestedType<WorkItemRetryProcessor>().TheDefault.Is.OfConcreteType<WorkItemRetryProcessor>()
    .CtorDependency<IWorkItemProcessor>().Is(x => x.OfConcreteType<SimpleWorkItemProcessor>())
    .WithCtorArg("busyDelay").EqualTo(TimeSpan.FromMilliseconds(20))
    .WithCtorArg("overallTimeout").EqualTo(TimeSpan.FromSeconds(60));

ForRequestedType<WorkItemQueue>().TheDefault.Is.OfConcreteType<WorkItemQueue>()
    .CtorDependency<IWorkItemProcessor>().Is(x => x.OfConcreteType<WorkItemRetryProcessor>());

事实上,它说 IWorkItemProcessor 没有默认实例(这是正确的)。将最后一行切换为:

ForRequestedType<IWorkItemProcessor>().TheDefault.Is.OfConcreteType<WorkItemQueue>()
    .CtorDependency<IWorkItemProcessor>().Is(x => x.OfConcreteType<WorkItemRetryProcessor>());

...产生堆栈溢出异常。

您如何将既实现接口又在构造函数中采用相同接口的类链接在一起?

4

1 回答 1

0

这行得通,但我无法解释为什么。据我所知,第一个版本应该也能正常工作。

ForRequestedType<SimpleWorkItemProcessor>().TheDefault.Is.OfConcreteType<SimpleWorkItemProcessor>();

var retryProcessor = ForRequestedType<WorkItemRetryProcessor>().TheDefault.Is.OfConcreteType<WorkItemRetryProcessor>()
    .CtorDependency<IWorkItemProcessor>().Is(x => x.OfConcreteType<SimpleWorkItemProcessor>())
    .CtorDependency<TimeSpan>("busyDelay").Is(x => x.Object(TimeSpan.FromMilliseconds(20)))
    .CtorDependency<TimeSpan>("overallTimeout").Is(x => x.Object(TimeSpan.FromSeconds(60)));

ForRequestedType<IWorkItemProcessor>().TheDefault.Is.OfConcreteType<WorkItemQueue>()
    .CtorDependency<IWorkItemProcessor>("workItemProcessor").Is(x => x.Instance(retryProcessor));
于 2009-09-18T13:42:48.823 回答