简体   繁体   中英

Extend Ninject Bind method

Ninject has this method .

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?

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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