Need help implementing Command Handlers/Bus using Unity DI

时间秒杀一切 提交于 2020-01-03 02:04:17

问题


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?


回答1:


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.


Update

You mean you would like to have something like this:

var builder = new ConfigurationBuilder();
var convention = new CommandHandlerConvention().WithTransaction().WithDeadlockRetry();
builder.Extension<DecoratorExtension>();
builder.Scan(x =>
{
    x.With(convention);
    x.AssemblyContainingType(typeof(BarCommand));
});
var container = new UnityContainer();
container.AddExtension(builder);
ICommandHandler<BarCommand> barHandler = container.Resolve<ICommandHandler<BarCommand>>("BarHandler");
var command = new BarCommand();
barHandler.Handle(command);
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.


Update2

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



来源:https://stackoverflow.com/questions/10002154/need-help-implementing-command-handlers-bus-using-unity-di

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!