Need help implementing Command Handlers/Bus using Unity DI

Go To


Folks, I am trying to re-factor a legacy brownfield application into a CQRS architecture with commands and a command bus for domain modifications.

The application will more than likely be implemented in Asp.Net MVC3. My employer prefers the use of Unity for DI in MVC applications.

Any examples I can find showing a dependency container for command/bus resolution are based on Structuremap or Autofac, however I will need to use Unity in this implementation. Has anyone used Unity in this manner or know of any examples?

2012-04-03 22:08
by mikelus
This will probably be very hard to achieve with Unity. See here: - Steven 2012-04-03 22:50


Where exactly do you think you need the container at all? Maybe this post contains some usefull information.

It describes a container agnostic way of handling commmands.


You mean you would like to have something like this:

var builder = new ConfigurationBuilder();
var convention = new CommandHandlerConvention().WithTransaction().WithDeadlockRetry();
builder.Scan(x =>
var container = new UnityContainer();
ICommandHandler<BarCommand> barHandler = container.Resolve<ICommandHandler<BarCommand>>("BarHandler");
var command = new BarCommand();
Assert.AreEqual("-->Retry-->Transaction-->BarHandler", command.HandledBy);

That registration uses a custom configuration engine for Unity that provides a lot of the features of StructureMap's config.


The code samples are part of my pet project on codeplex. The above snippets can be found inside the TecX.Unity.Configuration.Test project.

2012-04-04 06:13
by Sebastian Weber
Although Mark Seemann's article clearly shows how to build an message dispatching architecture, shows how to prevent the Service Locator anti-pattern, is by itself very interesting, and shows this without using a container, this will probably not help the OP. It is almost impossible to keep the composition root maintainable without a container, when working on an application that is designed around generic interfaces such as ICommandHandler<T> and IEventHandler<T>. The manual wiring is simply too painful. So the OPs question remains: how to do this with Unity - Steven 2012-04-04 07:29
@Sebastian I am looking for a simple and manageable way to wire up commands and command handlers. A simple synchronous bus would probably be a way to go but the implementations I have seen such as NServiceBus are overkill my needs - mikelus 2012-04-04 09:09
@Steven Please see my update. Btw.: what's the meaning of OP - Sebastian Weber 2012-04-04 11:20
OP means something like 'original poster', or is at least refering to the person asking the question. I don't know where this comes from, but this abbreviation is used a lot here at SO, Stackoverflow ;- - Steven 2012-04-04 11:21
Are ConfigurationBuilder, CommandHandlerConvention, and DecoratorExtension part of Unity? If not, where are they from - Steven 2012-04-04 11:25
@Steven They are from my pet project on codeplex. It contains a rather large set of extensions for Unity. The above code snippets can be found inside the TecX.Unity.Configuration.Test project - Sebastian Weber 2012-04-04 11:34
@SebastianWeber: Can you add that link to your answer - Steven 2012-04-04 11:51