我阅读了以下文章.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<>));
答案 0 :(得分:3)
这是你的类声明的样子......
public class CheckoutCustomerCommandHandler :
ICommandHandler<CheckoutCustomerCommand> {...}
public class MoveCustomerCommandHandler :
ICommandHandler<MoveCustomerCommand> {...}
这些看起来可能实现了相同的接口,但实际上它们编译为两个不同的接口,因为泛型参数不同。您的DI框架将能够区分它们。