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

  • Home
  • SEARCH
  • 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 3755178
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:39:30+00:00 2026-05-19T09:39:30+00:00

I have the following: public interface ICommand { } public class AddUser : ICommand

  • 0

I have the following:

public interface ICommand { }
public class AddUser : ICommand
{
    public string Name { get; set; }
    public string Password { get; set; }
}

public interface ICommandHandler<T> : IHandler<T> where T : ICommand
{
    void Execute(T command);
}

public class AddUserHandler : ICommandHandler<AddUser>
{
    public void Execute(AddUser command)
    {
        Console.WriteLine("{0}: User added: {1}", GetType().Name, command.Name);
    }
}

public class AuditTrailHandler : ICommandHandler<ICommand>
{
    public void Execute(ICommand command)
    {
        Console.WriteLine("{0}: Have seen a command of type {1}", GetType().Name, command.GetType().Name);
    }
}

I would like to use scanning to register the ICommandHandler<> so that I get the following types in the container:

  • ICommandHandler<AddUser> with concrete type AddUserHandler
  • ICommandHandler<AddUser> with concrete type AuditTrailHandler

I have tried this with an implementation of IRegistrationConvention and at one point I had it working but I just cannot get my head around how I did it.

The goal is to be able to execute several handlers for a specific ICommand implementation like so:

// A method in CommandDispatcher
public void SendCommand<T>(T command) where T : ICommand {
   var commandHandlers = container.GetAllInstances<ICommandHandler<T>>();
   foreach (var commandHandler in commandHandlers) {
       commandHandler.Execute(command);    
   }
}

I want the AuditTrailHandler<ICommand> to execute for all concrete implementations of ICommand, hence the need to register them for all types of ICommand.

Secondary objective would be if I could inject a collection of ICommandHandlers<ICommand> into my CommandDispatcher instead of the container, but I think that’s impossible with the structure I have now. Prove me wrong if you have any ideas.

EDIT – solved

I added a non generic interface that my generic interface implements and then I also added an Abstract CommandHandler<T> so I don’t have to implement the CanHandle or Execute(object) methods in all my handlers.

This is the working structure:

public interface ICommandHandler
{
    void Execute(object command);
    bool CanHandle(ICommand command);
}

public interface ICommandHandler<T> : ICommandHandler, IHandler<T> where T : ICommand
{
    void Execute(T command);
}

public abstract class AbstractCommandHandler<T> : ICommandHandler<T> where T : ICommand
{
    public abstract void Execute(T command);
    public void Execute(object command)
    {
        Execute((T)command);
    }

    public virtual bool CanHandle(ICommand command)
    {
        return command is T;
    }
}

And since this originally was a StructureMap question, here is the Scan to add this:

Scan(x =>
        {
            x.AssemblyContainingType<MyConcreteHandler>();
            x.IncludeNamespaceContainingType<MyConcreteHandler>();
            x.AddAllTypesOf<ICommandHandler>();
            x.WithDefaultConventions();
        });

This makes me able to inject an IEnumerable in my CommandDispatcher and execute like so:

public void SendCommand<T>(T command) where T : ICommand
    {
        var commandHandlersThatCanHandle = commandHandlers.Where(c => c.CanHandle(command));
        foreach (var commandHandler in commandHandlersThatCanHandle)
        {
            commandHandler.Execute(command);    
        }
    }

This makes me able to execute CommandHandlers that support AddUser (like the AddUserHandler), but I’m also able to execute a handler that support ICommand (like the AuditTrailHandler).

This is sweet!

  • 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-19T09:39:31+00:00Added an answer on May 19, 2026 at 9:39 am

    To have all of the command handlers injected into the command dispatcher, create a new, non-generic ICommandHandler interface which ICommandHandler<T> derives from. It has an Execute method which takes an object. The downside is that each of your command handlers has to implement that method to call the typed overload:

    public class AddUserHandler : ICommandHandler<AddUser>
    {
        public void Execute(object command)
        {
            Execute((AddUser)command);
        }
        public void Execute(AddUser command)
        {
            Console.WriteLine("{0}: User added: {1}", GetType().Name, command.Name);
        }
    }
    

    This will enable your command dispatcher to have a constructor dependency on IEnumerable<ICommandHandler>, which StructureMap will automatically populate.

    In SendCommand you have 2 ways of getting the appropriate set of handlers. A simple filter based on type:

    commandHandlers.OfType<ICommandHandler<T>>
    

    or add a CanHandle(object command) to the ICommandHandler interface:

    commandHandlers.Where(x => x.CanHandle(command))
    

    The second approach requires more code, but gives you a little more flexibility by allowing you to attach handler by more than just type. This may make solving your first problem easier, by making your AuditTrailHandler always return true from CanHandle.

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

Sidebar

Related Questions

I have the following: public interface ICartItem { string Name { get; set; }
I have the following code files: public interface IMod { string Name { get;
I have the following interface public interface IHandler<T> { void Handle(T myObject); } I'd
I have the following Interfaces: public interface ITemplateItem { int Id { get; set;
I have following code public interface IEntity { int Id { get; set; }
I have the following interface: public interface ILogger { void Debug(string message, params object[]
I have defined the following interface: public interface IHaveAProblem { string Issue { get;
Have following code: public interface ITest { string St1 { get; } } public
Hi have the following interface: public interface KeyValueStore { public void put(String key, byte[]
I have following component composition: public interface IJob { ILogger Logger { get; set;

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.