1

Im not sure how to state this question, because im not sure where the problem lies. I think its a genetric covariance issue, but the solution might be found somewhere else, maybe in the way the interfaces are designed, or in how the implementations are registered.

Anyway, the sample is trying to register all types implementing a generic interface, then later resolve the type using the type of the generic. Then trying to cast this type to its base type to be able to call a method on that implementation.

Its now failing when trying to cast. As an example, the first line of code fails to compile. When removed, the program fails on the line trying to cast the implementation.

class Program
{
    private static IContainer _container;

    static void Main(string[] args)
    {
        // Is this the problem?
        IHandler<IConfiguration> test = new MyHandler();

        // setup ioc
        var builder = new ContainerBuilder();
        builder.RegisterAssemblyTypes(typeof(Program).Assembly)
            //.Where(t => typeof(IHandler<IConfiguration>).IsAssignableFrom(t));
            .Where(t => t.GetInterfaces()
                .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IHandler<>))
                .Any()
                )
            .As(t => t.GetInterfaces()
                .Where(i => i.GetGenericTypeDefinition() == typeof(IHandler<>))
                .Single()
            );
        _container = builder.Build();

        // get my configuration impl
        var configuration = new MyConfiguration();

        // resolve handler for the configuration
        var configurationType = configuration.GetType();
        var handlerGenericType = typeof(IHandler<>);
        var handlerType = handlerGenericType.MakeGenericType(configurationType);
        var handler = _container.Resolve(handlerType);
        var typedHandler = (IHandler<IConfiguration>) handler;

        // handle it!
        typedHandler.Handle(configuration);
    }
}

public interface IConfiguration
{
}

public interface IHandler<T> where T : IConfiguration
{
    void Handle(T myConfiguration);
}

public class MyConfiguration : IConfiguration
{
}

public class MyHandler : IHandler<MyConfiguration>
{
    public void Handle(MyConfiguration myConfiguration)
    {
        Console.WriteLine("Handling my stuff...");
    }
}

1 Answer 1

2

There is no way this is going to work.
Here is the reason why:
An IHandler<IConfiguration> requires an IConfiguration as parameter to Handle.
If your first line would be valid, the following would compile:

MyOtherConfiguration config = new MyOtherConfiguration();
IHandler<IConfiguration> test = new MyHandler();
test.Handle(config);

Obviously, this is not correct, because MyHandler.Handle wants a MyConfiguration or derived type.

Using contra-variance as described in my earlier version of this answer would allow you to do the following:

IHandler<MyDerivedConfiguration> test = new MyHandler();

with MyDerivedConfiguration being derived from MyConfiguration.

5
  • Tried that, and it still doesnt work. Tried out T aswell, then the assignment works, but not the implementation of the handler wont compile.
    – MatteS
    Commented Aug 8, 2011 at 11:59
  • Thanks alot, now its completely clear to me why this doesnt work. I think ill have to rethink my design. Or if not that, maybe its feasable to call my handler using reflection.
    – MatteS
    Commented Aug 8, 2011 at 13:32
  • After some googling I see a similar design in the ConsumerOf<T> interface in Rhino Service Bus. I wonder how they solve calling the consumer..
    – MatteS
    Commented Aug 8, 2011 at 13:33
  • @MatteS: I can't see how ConsumerOf<T> tries to do the same you want. Commented Aug 8, 2011 at 14:05
  • Well, the similarities I see is that ConsumerOf<T> is not that big a difference than my IHandler<T>. Both interfaces has a method which has a varaible of T passed to it aswell. Also, the the implementors are in both cases loaded into an ioc container, then later resolved and their respective methods called.
    – MatteS
    Commented Aug 8, 2011 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.