Reputation: 15003
I fail to understand how to probably setup my Ninject IoC container.
I have a Service layer which contains implementations of several services and implementation of my DbContext
and ASP.NET Identity like so:
public class IdentityModule : NinjectModule
{
public override void Load()
{
Bind<IUserStore<User, int>>().To<UserService>().InSingletonScope();
Bind<UserManager<User, int>>().ToSelf().InSingletonScope();
Bind<IRoleStore<UserRole, int>>().To<UserRoleService>().InSingletonScope();
Bind<RoleManager<UserRole, int>>().ToSelf().InSingletonScope();
}
}
public class EntityFrameworkModule : NinjectModule
{
public override void Load()
{
Bind<EntityDbContext>().ToSelf();
Bind<ICreateDbModel>().To<DefaultDbModelCreator>();
Bind<IUnitOfWork>().To<EntityDbContext>();
Bind<IWriteEntities>().To<EntityDbContext>();
Bind<IReadEntities>().To<EntityDbContext>();
}
}
These are loaded into my NinjectWebCommen
(MVC layer):
private static void RegisterServices(IKernel kernel)
{
var modules = new INinjectModule[]
{
new EntityFrameworkModule(),
new IdentityModule()
};
kernel.Load(modules);
}
Now my question is:
My EntityDbContext
should be request scoped, but I am unable to set .InRequestScope()
from my service layer. Should this INinjectModule
then be moved to the MVC layer instead of lying in the service layer or should I reference Ninject.Web.Common
in my service layer? This just seem to be a wrong way since the service layer is not a web-app.
Upvotes: 1
Views: 988
Reputation: 4273
This place where you wire everything together is called the Composition Root in DI terminology
One of the suggestions is to create a bootstrap for all layers. Check this SO:
Where to locate Ninject modules in a multi-tier application
Upvotes: 1
Reputation: 879
This is not the best answer but I have been having the same problem. First, make sure Ninject and Ninject.Web.Common are referenced in the service layer, and that you have a using statement for Ninject.Web.Common in your module class.
I couldn't find InRequestScope in Intellisense. I tried a few times opening and closing the project, and it finally showed up. Didn't do anything but close and re-open Visual Studio and it started working.
Upvotes: 0