views:

70

answers:

1

This has completely baffled me on a number of configurations. I keep reading the documentation, and I just don't get it. Here is my registration code:

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>());

As it is, it says there's no default instance for IWorkItemProcessor (which is correct). Switching the last line to this:

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

...Makes a stack overflow exception.

How do you chain classes together that both implement an interface, and take in that same interface in their constructor?

A: 

This works, but I can't explain why. From what I know, the first version should work just as well.

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));
Jason Young