Ninject - Multiple Classes Using Single Interface (more than one matching bindings are available)

J Hunt

If I have an implementation of Human and Dog class which uses the IPerson interface and HumanFood and DogFood class using the IFood interface. How can I switch from using HumanFood to DogFood and Human to Dog in my main function?

Currently the way this is written it is giving me a "more than one matching bindings are available" error.

Thanks!

public class Bindings : NinjectModule
{
    public override void Load()
    {
        this.Bind<IFood>().To<HumanFood>();
        this.Bind<IFood>().To<DogFood>(); 
        this.Bind<IPerson>().To<Human>();
        this.Bind<IPerson>().To<Dog>(); 
    }
}

static void Main(string[] args)
{
    IKernel kernel = new StandardKernel();
    kernel.Load(Assembly.GetExecutingAssembly());

    IFood food = kernel.Get<IFood>();
    IPerson person = kernel.Get<IPerson>();
    person.BuyFood();

    Console.ReadLine();
}
Steve Lillis

Typical ways to do this is to either use named binding:

this.Bind<IFood>().To<HumanFood>().Named("HumanFood");

Or to determine the binding to use based on WhenInjectedInto:

this.Bind<IFood>().To<HumanFood>().WhenInjectedInto<Human>();
this.Bind<IFood>().To<DogFood>().WhenInjectedInto<Dog>();

However, both of these represent a code smell. You may want to rethink why you're injecting varying implementations depending on the destination and perhaps inject an implementation of the factory pattern instead.

A handy overview of some of the stuff you can do can be found here:

http://lukewickstead.wordpress.com/2013/02/09/howto-ninject-part-2-advanced-features/

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

子查询结果 'Single Row Sub-Query results more than One Row'

来自分类Dev

Crouton with more than one OS

来自分类Dev

Mapping instance to object in Ninject bindings

来自分类Dev

Remove more than one view on PagerAdapter

来自分类Dev

Find matching type for one of multiple joined tables

来自分类Dev

In Domain Driven Design, may a transaction modify more than one aggregate?

来自分类Dev

How can I populate more than one zendform?

来自分类Dev

How to find sets of rows with one or more fields matching and assign a set id for each matching set?

来自分类Dev

Multiple UPDATE in single transaction vs one UPDATE with big WHERE clause

来自分类Dev

Visual Studio Code: "Program has more than one entry point defined"

来自分类Dev

SQL query for selecting foreign-key rows with more than one link to the primary-key table

来自分类Dev

Select rows where column contains same data in more than one record

来自分类Dev

Error while copying more than 8202 characters from one cell to another

来自分类Dev

"List elements that take up more than one line are not permitted" error with complex IF statement

来自分类Dev

Prefer matching search term in beginning of search result rather than in the end using elasticsearch

来自分类Dev

How to bind using Ninject Conventions Extension?

来自分类Dev

Multiple functions to write into a single log file using Python

来自分类Dev

How to update multiple rows using single where clause

来自分类Dev

How to find the occurrences of any word with more than two vowels in a file using regex?

来自分类Dev

Pattern matching multiple variables

来自分类Dev

mySQL: pattern matching 3 or more "a's" in name

来自分类Dev

Connecting multiple wireless devices to one main device using bluetooth

来自分类Dev

Multiple dropzone with single function

来自分类Dev

How to Select more than WMI Query

来自分类Dev

How to use $compile service more than once?

来自分类Dev

Merge Statement for more than hundreds of columns

来自分类Dev

Multiple Selects into one select

来自分类Dev

reformatting report file using linux shell commands combining multiple lines output into one

来自分类Dev

Ninject的RepositoryFactory

Related 相关文章

  1. 1

    子查询结果 'Single Row Sub-Query results more than One Row'

  2. 2

    Crouton with more than one OS

  3. 3

    Mapping instance to object in Ninject bindings

  4. 4

    Remove more than one view on PagerAdapter

  5. 5

    Find matching type for one of multiple joined tables

  6. 6

    In Domain Driven Design, may a transaction modify more than one aggregate?

  7. 7

    How can I populate more than one zendform?

  8. 8

    How to find sets of rows with one or more fields matching and assign a set id for each matching set?

  9. 9

    Multiple UPDATE in single transaction vs one UPDATE with big WHERE clause

  10. 10

    Visual Studio Code: "Program has more than one entry point defined"

  11. 11

    SQL query for selecting foreign-key rows with more than one link to the primary-key table

  12. 12

    Select rows where column contains same data in more than one record

  13. 13

    Error while copying more than 8202 characters from one cell to another

  14. 14

    "List elements that take up more than one line are not permitted" error with complex IF statement

  15. 15

    Prefer matching search term in beginning of search result rather than in the end using elasticsearch

  16. 16

    How to bind using Ninject Conventions Extension?

  17. 17

    Multiple functions to write into a single log file using Python

  18. 18

    How to update multiple rows using single where clause

  19. 19

    How to find the occurrences of any word with more than two vowels in a file using regex?

  20. 20

    Pattern matching multiple variables

  21. 21

    mySQL: pattern matching 3 or more "a's" in name

  22. 22

    Connecting multiple wireless devices to one main device using bluetooth

  23. 23

    Multiple dropzone with single function

  24. 24

    How to Select more than WMI Query

  25. 25

    How to use $compile service more than once?

  26. 26

    Merge Statement for more than hundreds of columns

  27. 27

    Multiple Selects into one select

  28. 28

    reformatting report file using linux shell commands combining multiple lines output into one

  29. 29

    Ninject的RepositoryFactory

热门标签

归档