views:

77

answers:

1

I'm trying to use Windsor as a factory to provide specification implementations based on subtypes of XAbstractBase (an abstract message base class in my case).

I have code like the following:

public abstract class XAbstractBase { }
public class YImplementation : XAbstractBase { }
public class ZImplementation : XAbstractBase { }

public interface ISpecification<T> where T : XAbstractBase
{
    bool PredicateLogic();
}

public class DefaultSpecificationImplementation : ISpecification<XAbstractBase>
{
    public bool PredicateLogic() { return true; }
}

public class SpecificSpecificationImplementation : ISpecification<YImplementation>
{
    public bool PredicateLogic() { /*do real work*/ }
}

My component registration code looks like this:

container.Register(
    AllTypes.FromAssembly(Assembly.GetExecutingAssembly())
    .BasedOn(typeof(ISpecification<>))
    .WithService.FirstInterface()
)

This works fine when I try to resolve ISpecification<YImplementation>; it correctly resolves SpecificSpecificationImplementation.

However, when I try to resolve ISpecification<ZImplementation> Windsor throws an exception:

"No component for supporting the service ISpecification'1[ZImplementation, AssemblyInfo...] was found"

Does Windsor support resolving generic implementations down to base classes if no more specific implementation is registered?

A: 

See this post.

Update

Ok, I see now what you're doing wrong. You have no service for ISpecification<ZImplementation>, hence it's not surprising that Windsor can't resolve it.

It's not a Windsor issue at all.

Krzysztof Koźmic
That was one of the better posts I found before I posted the question here. What I'm looking for is whether it's possible to address the zero-or-more situation (the post only addresses the one-or-more situation, but I assume it would be trivial to add a default return value) using only Windsor's built-in facilities. If I have to write a factory anyway, I'm much more likely to just register it and inject it like normal.
arootbeer
Okay; I understand why it's not possible. Thanks!
arootbeer