Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6256037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:28:33+00:00 2026-05-24T14:28:33+00:00

First, sorry for the vague question title. I couldn’t come up with a more

  • 0

First, sorry for the vague question title. I couldn’t come up with a more precise one.

Given these types:

                                                     { TCommand : ICommand }
       «interface»                   «interface»    /
      +-----------+         +----------------------/----+
      | ICommand  |         | ICommandHandler<TCommand> |
      +-----------+         +---------------------------+
            ^               | Handle(command: TCommand) |
            |               +---------------------------+
            |                              ^
            |                              |
      +------------+            +-------------------+
      | FooCommand |            | FooCommandHandler |
      +------------+            +-------------------+
            ^
            |
   +-------------------+
   | SpecialFooCommand |
   +-------------------+

I would like to write a method Dispatch that accepts any command and sends it to an appropriate ICommandHandler<>. I thought that using a DI container (Autofac) might greatly simplify the mapping from a command’s type to a command handler:

void Dispatch<TCommand>(TCommand command) where TCommand : ICommand
{
    var handler = autofacContainer.Resolve<ICommandHandler<TCommand>>();
    handler.Handle(command);
}

Let’s say the DI container knows about all the types shown above. Now I’m calling:

Dispatch(new SpecialFooCommand(…));

In reality, this will result in Autofac throwing a ComponentNotRegisteredException, since there is no ICommandHandler<SpecialFooCommand> available.

Ideally however, I would still want a SpecialFooCommand to be handled by the closest-matching command handler available, ie. by a FooCommandHandler in the above example.

Can Autofac be customized towards that end, perhaps with a custom registration source?


P.S.: I understand that there might be the fundamental problem of co-/contravariance getting in the way (as in the following example), and that the only solution might be one that doesn’t use generics at all… but I would want to stick to generic types, if possible.

ICommandHandler<FooCommand> fooHandler = new FooCommandHandler(…);
ICommandHandler<ICommand> handler = fooHandler;
//                                ^
//              doesn't work, types are incompatible
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:28:34+00:00Added an answer on May 24, 2026 at 2:28 pm

    Not really a fair answer, as I’ve extended Autofac since you posted the question… 🙂

    As per Daniel’s answer, you’ll need to add the in modifier to the TCommand parameter of ICommandHandler:

    interface ICommandHandler<in TCommand>
    {
        void Handle(TCommand command);
    }
    

    Autofac 2.5.2 now includes an IRegistrationSource to enable contravariant Resolve() operations:

    using Autofac.Features.Variance;
    
    var builder = new ContainerBuilder();
    builder.RegisterSource(new ContravariantRegistrationSource());
    

    With this source registered, services represented by a generic interface with a single in parameter will be looked up taking variant implementations into account:

    builder.RegisterType<FooCommandHandler>()
       .As<ICommandHandler<FooCommand>>();
    
    var container = builder.Build();
    container.Resolve<ICommandHandler<FooCommand>>();
    container.Resolve<ICommandHandler<SpecialFooCommand>>();
    

    Both calls to Resolve() will successfully retrieve the FooCommandHandler.

    If you can’t upgrade to the latest Autofac package, grab the ContravariantRegistrationSource from http://code.google.com/p/autofac/source/browse/src/Source/Autofac/Features/Variance/ContravariantRegistrationSource.cs – it should compile against any recent Autofac build.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry about the vague title, but i didnt know how to ask the question
First of all, sorry for the vague title. Let me explain. At work we're
First of all, sorry if this question is a little vague and rambling! I'm
I'm sorry to make my first question here a bit of a simple one
Sorry for the relatively vague title, I couldn't think of anything else. So in
Sorry for the slightly noobish question, as I am writing my first rails app.
So I'm sorry the title is so vague I'm not really even sure how
First, sorry for the lengthy post. Basically, my question is this: I'm trying to
Sorry for the rather vague title, but it seemed befitting of a similarly vague
First let me say that I really feel directionless on this question. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.