How To Use Ninject Named Bindings With DependencyResolver and PropertyInjection

前端 未结 1 1164
轻奢々
轻奢々 2020-12-31 18:53

I realize constructor injection is preferred but I\'m curious how to use Ninject\'s contextual \'Named Bindings\' when using another form of injection.

Specifically

1条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-31 19:18

    You can create a named binding to work on Alpha:

    Bind().To().Named("Alpha");
    

    then you can specify others like:

    Bind().To().Named("Beta");
    

    When your example constructor is used you will get the AlphaRepository.

    In order to use the name with a property give the property a name attribute just like you you did for the param:

    [Inject, Named("Alpha")]
    public IRepository Foo {get; set;}
    

    0 讨论(0)
提交回复
热议问题