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 5990697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:16:51+00:00 2026-05-22T23:16:51+00:00

I want to use unity for a polymorphic event aggregation/handling where handlers are registered

  • 0

I want to use unity for a polymorphic event aggregation/handling where handlers are registered through the container.

Handlers declares a generic parameter with the “in” modifier, since it’s only used as an input parameter. So the parameter is contravariant.

Considering the following sample.

interface IHandler<in T> { void Handle(T message); }

interface IMsg { string Message { get; } }

class MsgA : IMsg { ... }

class MsgB : IMsg { ... }

class MsgAHandler : IHandler<MsgA> {
  public void Handle(MsgA msg) { ... }
}

class MsgBHandler : IHandler<MsgB> {
  public void Handle(MsgB msg) { ... }
}

class MsgHandler : IHandler<IMsg> {
  public void Handle(IMsg msg) { ... }
}

class ObjectHandler : IHandler<object> {
  public void Handle(object msg) { ... }
}

class Events {
   public void Publish<T>(T message) {
     // Actually this code is more complex for Unity, because it first tries to
     // resolve the default registered component using .Resolve(),
     // then use .ResolveAll() to resolve all named components
     foreach (var handler in this.container.ResolveAll<IHandle<T>>()) {
       handler.Handle(message);
     }
   }
}

My expectations are the followings :

  • If I receive a MsgA, I should dispatch it to MsgAHandler, MsgHandler and ObjectHandler.
  • If I receive a IMsg, I should dispatch it to MsgHandler and ObjectHandler.
  • ObjectHandler should receive all messages.

By default, Castle Windsor handles correctly this scenario without any specific configuration (tested with v2.5.1).

Does Unity exposes a way to enhance the resolution and allow Resolve<IHandle<MsgA>>() to also match any type implementing IHandle<IMsg> or IHandle<object> ?

I’ve looked for samples using UnityExtensions (matching rules, policies, and so on) without success.

Could someone directs me to the correct mechanism to use to implement a such scenario ?

  • 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-22T23:16:52+00:00Added an answer on May 22, 2026 at 11:16 pm

    Could you do:

    container.RegisterType(typeof(IHandle<>), typeof(MsgAHandler<>))
    

    But you would probably need to change some of your code for that to work properly. Like:

    public interface IHandler<in THandler> where THandler : class, IMsg
    { void Handle(THandler message); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to add an existing object to my unity container registered to a
we want to use Unity for IOC. All i've seen is the implementation that
I want to use dependency injection (Unity) and at the moment I'm thinking about
I want to use Unity resolve IService to two different implementations, to make use
I want to use Unity interception for an interface without an implementation, so that
Sorry for this noob question. I'm beginning to use the Unity 2.0 container with
I want to Unit Test my application which use MSMQ but i found no
I want to unit test a RESTful interface written with Apache CXF. I use
I personally don't use FxCop yet. We want to work out the unit testing
I want use groovy findAll with my param to filtering closure filterClosure = {

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.