Ninject 是否具有我可以用来装饰类或构造函数以让 Ninject 忽略它的属性?
我需要摆脱:
检测到两个服务的构造函数之间存在周期性依赖关系。
这是我的代码:
// Abstraction
public interface ICommandHandler<TCommand>
{
void Handle(TCommand command);
}
// Implementation
public class ShipOrderCommandHandler
: ICommandHandler<ShipOrderCommand>
{
private readonly IRepository<Order> repository;
public ShipOrderCommandHandler(
IRepository<Order> repository)
{
this.repository = repository;
}
public void Handle(ShipOrderCommand command)
{
// do some useful stuf with the command and repository.
}
}
我的通用装饰器:
public TransactionalCommandHandlerDecorator<TCommand>
: ICommandHandler<TCommand>
{
private ICommandHandler<TCommand> decoratedHandler;
public TransactionalCommandHandlerDecorator(
ICommandHandler<TCommand> decoratedHandler)
{
this.decoratedHandler = decoratedHandler;
}
public void Handle(TCommand command)
{
using (var scope = new TransactionScope())
{
this.decoratedHandler.Handle(command);
scope.Complete();
}
}
}
我的容器注册:
kernel.Bind(typeof(ICommandHandler<>))
.To(typeof(TransactionCommandHandlerDecorator<>));