How do you use an injection method using Ninject?

I have a class that should use IRepository for one method in it class.

Ideally, I would like to avoid the need to allow this dependency in the class constructor, and so I found a method level injection in Ninject and wondered how it works?

I understand how to configure it. What am I embarrassed is to call it?

Example:

class SomeClassThatUsesRepository
{
    [Inject]
    public void QueryForSomeStuff(IRepository repository)
    {
        //do some stuff
    }
}

My problem is, how can I call this method without specifying an IRepository?

var someClass = Kernel.Resolve<SomeClassThatUsesRepository>();

will work if I use the constructor, but I want to call the method.

How do I call a method using the Ninject method?

+5
source share
2 answers

, - ( , ). , Java,

public void SetRepository(IRepository repository) { ... }

[Inject], , Ninject , IRepository .

, , QueryForSomeStuff , SomeClassThatUsesRepository.

+12

, . MVC .

[Ninject.Inject]
public void ResolveDI(ISettingStore store)
{
    ConfigHelper = store;
}

, ConfigHelper null, OnActionExecuting.

0

All Articles