"CommandBus" with Handlers & Observers I'm looking for a very simple (at first sight) CommandBus, which will handle some ICommand publication. The CommandBus implementation will find the appropriate IHandler to Execute the Command and then Notify some possible IObservers.

My first step is to make it work, I've chosen to use a synchronous dependency injection pattern which I may extends in the future, maybe using a real ServiceBus with some asynchronous capability.

Here is the abstract definition:

public interface ICommandBus
{
    TResult Publish&lt;TCommand, TResult&gt;(TCommand command)
        where TResult : ICommandResult
        where TCommand : ICommand&lt;TResult&gt;;
}

public interface ICommand&lt;T&gt;
    where T : ICommandResult
{
    int Id_User { get; }
}

public interface ICommandResult
{
    bool Success { get; }

    Exception Error { get; }

    ICollection&lt;ValidationRule&gt; BrokenRules { get; }
}

public interface ICommandHandler&lt;TCommand, TResult&gt;
    where TCommand : ICommand&lt;TResult&gt;
    where TResult : ICommandResult
{
    TResult Execute(TCommand command);
}

public interface ICommandObserver&lt;TCommand, TResult&gt;
    where TCommand : ICommand&lt;TResult&gt;
    where TResult : ICommandResult
{
    void Trigger(TCommand command, TResult result);
}


And here is the CommandBus implementation (using Structuremap as the DI)

public class CommandBus : ICommandBus
{
    private readonly IContainer m_container;

    public CommandBus(IContainer container)
    {
        m_container = container;
    }

    public TResult Publish&lt;TCommand, TResult&gt;(TCommand command)
        where TResult : ICommandResult
        where TCommand : ICommand&lt;TResult&gt;
    {
        using (MiniProfiler.Current.Step("CommandBus.Publish"))
        {
            var handler = m_container.GetInstance&lt;ICommandHandler&lt;TCommand, TResult&gt;&gt;();
            var observers = m_container.GetAllInstances&lt;ICommandObserver&lt;TCommand, TResult&gt;&gt;();

            var result = handler.Execute(command);

            foreach (var observer in observers)
                observer.Trigger(command, result);

            return result;
        }
    }
}


How to use:

public class CreateItemCommand: ICommand&lt;CreateItemResult&gt;
{
    public string Name { get; private set; }
}
public class CreateItemResult: ICommandResult
{
    public bool Success { get; private set; }
}

var command = new CreateItemCommand();
var result = commandBus.Publish&lt;CreateItemCommand, CreateItemResult&gt;(command);


What do you think about this pattern? Do you think it will be easy to upgrade? I'm afraid it can be an obvious bottleneck in my application...

Edit: I just want to past a link to my question about this pattern, in order to simplify all the Generics...
This is insanely nit-picky, but this is really just driving me nuts.


TResult Publish&lt;TCommand, TResult&gt;(TCommand command)



We read left to right from the return type to the name to the parameters, but you're ordered your generic types from param to result. I think this would be much easier to read as

TResult Publish&lt;TResult, TCommand&gt;(TCommand command)


It keeps the declaration closer to its use. Which is, admittedly, trivial in this case, but I think it makes the signature read more like a sentence.

On another note though, I really like that you're filtering the acceptable types. It reminds me that I need to do this more often myself. It's better than just trusting that you (or someone else most likely) knows what kind of types are okay to use generically.