An approach to the network messaging service I'm working on the communication module of an ecommerce application. This module takes care of handling messages that come from other apps over the wire.
The awful switch case is never an option so I decided to look for some pattern. While reading about this topic found that Message Factory and Double Dispatch patterns are the very useful in this scenario. I created my own version following those, but there is one issue I cannot figure out (maybe is ok the way I'm doing it). The things is: as I have one class responsible for handling each message, where should I put the code that register each message handler? In the code I've seen that logic is putted together in some method like in the code, but that will imply one line for each message handler, and maybe there is a shortcut for this. I thought about using reflection to discover Message Handlers at runtime but that would be a performance penalty just to avoid a couple lines of code that I can write I forget about them.
Any other idea?

Interface

/// &lt;summary&gt;
/// Represents the entity in charge of handling messages from other applications
/// &lt;/summary&gt;
interface ICommunicationService
{
    void RegisterHandler(IMessageHandler handler);

    void ProcessMessage(Message message);
}


Concrete implementation

class CommunicationService : ICommunicationService
{
    private readonly Dictionary&lt;int, IMessageHandler&gt; handlers = new Dictionary&lt;int, IMessageHandler&gt;();

    public void CommunicationService()
    {
        // this is what I want to avoid or improve.
        RegisterHandler(new LoginResponseMessageHandler());
        // ... a line for each handler should go next
    }

    public void RegisterHandler(IMessageHandler handler)
    {
        if (!handlers.ContainsKey(handler.Command))
            handlers.Add(handler.Command, handler);
        else
            handlers[handler.Command] = handler;
    }

    public void ProcessMessage(Message message)
    {
        // Incoming messages that does not have a handler registered will be ignored. This could be or not
        // the desired behavior.
        if (handlers.ContainsKey(message.Command))
            handlers[message.Command].ProcessMessage(message);
    }
}


Message Handlers

interface IMessageHandler
{
    /// &lt;summary&gt;
    /// Gets the identifier of the Command this handler will process.
    /// &lt;/summary&gt;
    int Command { get; }

    /// &lt;summary&gt;
    /// When implemented, should contain the logic for handling the requested command.
    /// &lt;/summary&gt;
    void ProcessMessage(Message message);
}


Example message handler

class LoginResponseMessageHandler : IMessageHandler
{
    public int MessageId
    {
        get { return 1; } // set the id of the message. this will identify the message across all apps.
    }

    public void ProcessMessage(Message message)
    {
        // here we receive the incomming message and we take care of it...
    }
}


PD: Looking at the code another question came to mind. As you can see I'm using an integer value to identify each message between all apps. Is that a good choice?
In addition to RubberDuck's answer, I'd also propose the following improvements.

I don't really see the need to do this:

if (!handlers.ContainsKey(handler.Command))
    handlers.Add(handler.Command, handler);
else
    handlers[handler.Command] = handler;


You can omit the if/else logic and simply assign the value:

handlers[handler.Command] = handler;


I also would advise to avoid a "negative" check in the if-clause. IMHO this is much easier to understand:

if (handlers.ContainsKey(handler.Command))
    handlers[handler.Command] = handler;
else
    handlers.Add(handler.Command, handler);


But like I said, the whole if/else logic isn't necessary in this case anyway; I'm just using it as an example.

Do not that your code currently doesn't prevent overwrites of existing key/value pairs. You might not want this; if you'd use this (instead of your four line if/else block):

handlers.Add(handler.Command, handler);


... then there'd be an exception if the same command is used more than once as a key.

I also advise against this:

if (handlers.ContainsKey(message.Command))
    handlers[message.Command].ProcessMessage(message);


And instead use TryGetValue to avoid unnecessary lookups:

IMessageHandler handler;
if(handlers.TryGetValue(message.Command, out handler)
{
    handler.ProcessMessage(message);
}