gpt4 book ai didi

.net - 如何指示 Ninject 从其隐式绑定(bind)列表中隐藏类型

转载 作者:行者123 更新时间:2023-12-04 05:51:14 26 4
gpt4 key购买 nike

Ninject 是否具有我可以用来装饰类或构造函数以让 Ninject 忽略它的属性?

我需要摆脱:

A cyclical dependency was detected between the constructors of two services.



这是我的代码:
// 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(TransactionCommandHandlerDecora‌​tor<>));

最佳答案

这是一个基于您的代码的工作示例。

public class AutoDecorationFacts
{
readonly StandardKernel _kernel = new StandardKernel();

public AutoDecorationFacts()
{
_kernel.Bind( typeof( ICommandHandler<> ) )
.To( typeof( TransactionalCommandHandlerDecorator<> ) )
.Named( "decorated" );
}

[Fact]
public void RawBind()
{
_kernel.Bind( typeof( ICommandHandler<> ) ).To<ShipOrderCommandHandler>().WhenAnyAnchestorNamed( "decorated" );
VerifyBoundRight();
}

void VerifyBoundRight()
{
var cmd = _kernel.Get<ICommandHandler<ShipOrderCommand>>();
Assert.IsType<TransactionalCommandHandlerDecorator<ShipOrderCommand>>( cmd );
}

最好与 Ninject.Extensions.Conventions 一起使用:-
    [Fact]
public void NameSpaceBasedConvention()
{
_kernel.Bind( scan => scan
.FromThisAssembly()
.SelectAllClasses()
.InNamespaceOf<CommandHandlers.ShipOrderCommandHandler>()
.BindAllInterfaces()
.Configure( x => x.WhenAnyAnchestorNamed( "decorated" ) ) );
VerifyBoundRight();
}

[Fact]
public void UnconstrainedWorksTooButDontDoThat()
{
_kernel.Bind( scan => scan
.FromThisAssembly()
.SelectAllClasses()
.BindAllInterfaces( )
.Configure( x=>x.WhenAnyAnchestorNamed("decorated" )));
VerifyBoundRight();
}
}

你的课:

公共(public)类 ShipOrderCommand
{
}
// Abstraction
public interface ICommandHandler<TCommand>
{
void Handle( TCommand command );
}

// Implementation
namespace CommandHandlers
{
public class ShipOrderCommandHandler
: ICommandHandler<ShipOrderCommand>
{
public ShipOrderCommandHandler(
)
{
}

public void Handle( ShipOrderCommand command )
{
// do some useful stuf with the command and repository.
}
}
}
public class TransactionalCommandHandlerDecorator<TCommand>
: ICommandHandler<TCommand>
{
private ICommandHandler<TCommand> decoratedHandler;

public TransactionalCommandHandlerDecorator(
ICommandHandler<TCommand> decoratedHandler )
{
this.decoratedHandler = decoratedHandler;
}

public void Handle( TCommand command )
{
this.decoratedHandler.Handle( command );
}
}

(使用 Ninject 和 Ninject.Extensions.Conventions 的 NuGet 最新版本)

关于.net - 如何指示 Ninject 从其隐式绑定(bind)列表中隐藏类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10013262/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com