【发布时间】:2012-10-22 10:12:38
【问题描述】:
我阅读了以下文章.NET Junkie - Meanwhile... on the command side of my architecture,该文章由另一位 stackoverflow 用户提出,该文章概述了命令模式,并在文章末尾提供了如何将其与 DI 一起使用的策略。
这有很大帮助,但我缺少一件事,假设我创建了一个名为 CheckoutCustomerCommandHandler 的新类。
现在,假设我需要通过构造函数将这个命令和MoveCustomerCommandHandler 注入到控制器中。这对 DI 容器设置和构造函数有何影响?
在核心上,它们都实现了相同的接口。这似乎会导致 DI 容器的查找问题。在文章示例中,这是他们的样品注射器设置:
public interface ICommandHandler<TCommand>
{
void Handle(TCommand command);
}
// Exactly the same as before, but now with the interface.
public class MoveCustomerCommandHandler
: ICommandHandler<MoveCustomerCommand>
{
private readonly UnitOfWork db;
public MoveCustomerCommandHandler(UnitOfWork db,
[Other dependencies here])
{
this.db = db;
}
public void Handle(MoveCustomerCommand command)
{
// TODO: Logic here
}
}
// Again, same implementation as before, but now we depend
// upon the ICommandHandler abstraction.
public class CustomerController : Controller
{
private ICommandHandler<MoveCustomerCommand> handler;
public CustomerController(
ICommandHandler<MoveCustomerCommand> handler)
{
this.handler = handler;
}
public void MoveCustomer(int customerId,
Address newAddress)
{
var command = new MoveCustomerCommand
{
CustomerId = customerId,
NewAddress = newAddress
};
this.handler.Handle(command);
}
}
using SimpleInjector;
using SimpleInjector.Extensions;
var container = new Container();
// Go look in all assemblies and register all implementations
// of ICommandHandler<T> by their closed interface:
container.RegisterManyForOpenGeneric(
typeof(ICommandHandler<>),
AppDomain.CurrentDomain.GetAssemblies());
// Decorate each returned ICommandHandler<T> object with
// a TransactionCommandHandlerDecorator<T>.
container.RegisterDecorator(typeof(ICommandHandler<>),
typeof(TransactionCommandHandlerDecorator<>));
// Decorate each returned ICommandHandler<T> object with
// a DeadlockRetryCommandHandlerDecorator<T>.
container.RegisterDecorator(typeof(ICommandHandler<>),
typeof(DeadlockRetryCommandHandlerDecorator<>));
【问题讨论】:
-
看起来这是一个
SimpleInjector特定的问题。我可以告诉你其他 DI 容器,如 Ninject 和 Autofac 支持这种情况。
标签: c# asp.net-mvc-3 dependency-injection command-pattern simple-injector