Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use method injection with Ninject?

Tags:

ninject

I have a class which needs to use an IRepository for one method in it's class.

Ideally, I would like to avoid having to resolve this dependency into the class's constructor, and so I found method level injection in Ninject and was wondering how this works?

I understand how to set it up. What I'm confused about is how to call it?

Example:

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

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

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

would work if I was using the constructor, but I want to call a method.

How do I call a method using Ninject method injection?

like image 740
Joseph Avatar asked Nov 03 '10 20:11

Joseph


People also ask

What is ninject dependency injection?

Ninject is a lightweight dependency injection framework for . NET applications. It helps you split your application into a collection of loosely-coupled, highly-cohesive pieces, and then glue them back together in a flexible manner.

How do you inject dependency injection?

Types of Dependency Injection The injector class injects dependencies broadly in three ways: through a constructor, through a property, or through a method. Constructor Injection: In the constructor injection, the injector supplies the service (dependency) through the client class constructor.

How does constructor injection work?

When a class requires an instance of a Dependency, you can supply that Dependency through the class's constructor, enabling it to store the reference for future use. Constructor Injection is the act of statically defining the list of required Dependencies by specifying them as parameters to the class's constructor.

Where do we use dependency injection?

More specifically, dependency injection is effective in these situations: You need to inject configuration data into one or more components. You need to inject the same dependency into multiple components. You need to inject different implementations of the same dependency.


1 Answers

I'm afraid method injection doesn't work this way - it's just one of the ways to inject dependencies into an object during its construction (you can inject your dependencies through constructor parameters, through properties, fields or methods). Method injection is useful if your class takes its dependencies by Java-style setter methods like

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

If it is marked with [Inject] attribute, you don't need to call this methods directly, it is to be called by Ninject during the initialization to pass the IRepository object into your resolved object.

So I believe your QueryForSomeStuff method is being called when you resove your SomeClassThatUsesRepository.

like image 75
NOtherDev Avatar answered Sep 21 '22 11:09

NOtherDev