Skip to content

5. Setup Ninject IoC

codeplanner edited this page Dec 30, 2014 · 3 revisions

Generating the Ninject Module

We need to tell Ninject how to map interfaces to classes. This is done automatically by calling the scaffolder for Ninject in the Package Manager Console.

PM> Scaffold Bob.Ninject.Module

This command will not create any files, just outpur some code into the the Package Manager Console.

public class ServiceModule : NinjectModule
{
    public override void Load()
    {
        Bind<IUnitOfWork>().To<UnitOfWork>().InThreadScope();
        Bind<IDatabaseFactory>().To<DatabaseFactory>().InThreadScope();
        Bind<IPersonRepository>().To<PersonRepository>().InThreadScope();
        Bind<IPersonService>().To<PersonService>().InThreadScope();
    }
}

Now you will need to install Ninject into the project where you want to use the IPersonService. You install different Ninject packages depending on what project type you are using, in this sample I will just install the basic Ninject package.

PM> Install-Package Ninject

Then just copy the class ServiceModule that was generated in the step above into the project where you installed Ninject.

Next step test the service.

Clone this wiki locally