简体   繁体   English

扩展Ninject绑定方法

[英]Extend Ninject Bind method

Ninject has this method . Ninject有这种方法

public IBindingToSyntax<T1, T2, T3, T4> Bind<T1, T2, T3, T4>()

Is it possible somehow extend this method to bind 5 interfaces to one implementation? 是否有可能以某种方式扩展此方法以将5个接口绑定到一个实现?

Like this: 像这样:

public IBindingToSyntax<T1, T2, T3, T4, T5> Bind<T1, T2, T3, T4, T5>()

Ninject provides a fluent syntax that allows you to bind up to 4 interfaces together to the same instance: Ninject提供了一种流利的语法,使您最多可以将4个接口绑定到同一实例:

Bind<IInterface1, IInterface2, IInterface3, IInterface4>().To<Implementation>();

If we need more interfaces there is a workaround: 如果我们需要更多接口,则有一种解决方法:

var bindingConfiguration =
    Bind<IInterface1, IInterface2, IInterface3, IInterface4>()
        .To<Implementation>()
        .BindingConfiguration;
kernel.AddBinding(new Binding(typeof(IInterface5), bindingConfiguration));

Below I posted a quote from author why only four interfaces? 下面我发表了作者的引文,为什么只有四个接口?

Some may ask what is if I want to bind more than four interface to the same service. 有人可能会问我是否要将四个以上的接口绑定到同一服务。 In a small discussion we came to the conclusion that if you have more than four interfaces on a single service than most likely you have a problem with the single responsibility principle and should fix this in first place. 在一次简短的讨论中,我们得出的结论是,如果单个服务上的接口数量超过四个,则您最有可能遇到单一责任原则的问题,应首先解决此问题。

Link to article New Features and changes of Ninject 3.0 链接到文章Ninject 3.0的新功能和更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM