Ninject dependency injection for more than one web project

Asked

Viewed 344 times

1

Hello, in an application I have a MVC web project and a Web API project, in the MVC project I already have the Ninject configured with the dependency injections and I would like to take advantage of it in this Web API project, I know I may have to separate itit in a project for injection of dependency to be able to reference it in the two projects, but my doubt is of how to do this.

Would anyone know to tell me?

1 answer

4


You can use Mudules as explained in the documentation:

https://github.com/ninject/Ninject/wiki/Modules-and-the-Kernel

Basically you create a Module in a DLL part, like this:

public class WarriorModule : NinjectModule
{
    public override void Load() 
    {
        Bind<IWeapon>().To<Sword>();
        Bind<Samurai>().ToSelf().InSingletonScope();
    }
}

And calls in both projects (MVC and Web Api):

 IKernel kernel = new StandardKernel(new WarriorModule());

 IDependency dependency = kernel.Get<IDependency>();
  • Just to clarify Fernando, doing this I would have to configure Ninject in the two projects, MVC and Webapi, and load the module in both?

  • Exact, the "configuration" is minimally duplicated, but the module is shared.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.