Neilski
Neilski

Reputation: 4415

Advice on Ninject Implementation

I'm developing my first project using Ninject (an MVC web application) and have a question regarding the correct/best use on Ninject.

I have set up a NinjectModule that binds an interface to a concrete class, but now I want to create instances of the interface object within my code. To achieve this I have written the following method:

public class NinjectControllerFactory : DefaultControllerFactory {

  private class MyServices : NinjectModule {
     ...
  }

   public static IMyRepository GetMyRepository()
   {
      IKernel kernel = new StandardKernel(new MyServices());
      return kernel.Get<IMyRepository>();
   } 
}

and this seems to work fine...

IMyRepository tempDB = ControllerFactory.GetRoomarRepository();

My question is, this the right/best way to achieve the result I'm looking for? I'm guess I'm concerned about the overhead of creating the Kernel instance.

Upvotes: 1

Views: 436

Answers (1)

Remo Gloor
Remo Gloor

Reputation: 32725

Read the documentation: https://github.com/ninject/ninject.web.mvc/wiki/MVC3 and look at the sample project.

Upvotes: 2

Related Questions