Ninject-创建带有多个注入的工厂

时间:2019-02-09 03:01:32

标签: ninject ioc-container

我有一个具有两个不同实现类的接口:

Bind<IRoundRobinMaker>().To<RoundRobinMaker>().Named("stdRoundRobin");
Bind<IRoundRobinMaker>().To<RoundRobinForcedCloseMaker>().Named("forcedClose");

我创建了一个工厂以传递一些参数(仅适用于RoundRobinForcedCloseMaker):

public interface IRoundRobinClosedForcedFactory
{
    IRoundRobinMaker CreateFactory(int rrNumber, int qualifiedPlayers);
}

并将其设置为Ninject模块:

Bind<IRoundRobinClosedForcedFactory>().ToFactory();

然后我通过WebApi在一个类中注入了工厂:

public class TournamentCloser 
{
    private readonly IRoundRobinClosedForcedFactory _factory;

    public TournamentCloser(IRoundRobinClosedForcedFactory factory)
    {
        _factory = factory;
    }

    public void ForceClose(int id, int rrNumber, int qualifiedPlayers)
    {
        var fact = _factory.CreateFactory(rrNumber, qualifiedPlayers);
        fact.CreateRoundRobin(tournament.players);
    }
}

我正确收到此错误:

Error activating IRoundRobinMaker
More than one matching bindings are available.
Matching bindings:
1) binding from IRoundRobinMaker to RoundRobinMaker
2) binding from IRoundRobinMaker to RoundRobinForcedCloseMaker

    Activation path:
     1) Request for IRoundRobinMaker

    Suggestions:
     1) Ensure that you have defined a binding for IRoundRobinMaker only once

我的工厂必须如何创建IRoundRobinMaker接口,该接口将注入RoundRobinForcedCloseMaker类?

0 个答案:

没有答案