Reputation: 3129
I'm using Ninject for DI and injecting IDbContext to my repositories as constructor parameter. I get a "The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects." error while trying to do something like this:
This is my controller's action method:
public ActionResult BindSpace(int spaceId, int managerId)
{
Space space = _spaceService.GetSpace(spaceId);
Manager manager = _managerService.GetManager(managerId);
if (space != null && manager != null)
{
_spaceService.BindManager(space, manager);
}
return RedirectToAction("GetSpaceBindingForm", new { id = space.Id });
}
This is the service method:
public void BindManager(Space space, Manager manager)
{
if (space != null && manager != null)
{
space.Managers.Add(manager);
_spaceRepo.Update(space);
}
}
There was no problem while adding and updating non-related entities.
There is no problem when I use:
ninjectKernel.Bind<IDbContext>().To<SPBSObjectContext>().InSingletonScope().WithConstructorArgument("nameOrConnectionString", "ShoppingPointBrowsingSystem");
I searched the web and everyone implements and uses NinjectModule abstract base class, but I have the following code. What am I doing wrong here?
This is the injection part:
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
}
private void AddBindings()
{
// HTTP Context
ninjectKernel.Bind<HttpContextBase>().ToMethod(context => new HttpContextWrapper(HttpContext.Current));
// Context
ninjectKernel.Bind<IDbContext>().To<SPBSObjectContext>().InRequestScope().WithConstructorArgument("nameOrConnectionString", "ShoppingPointBrowsingSystem");
// Repositories
ninjectKernel.Bind<IRepository<Admin>>().To<EfRepository<Admin>>().InRequestScope();
ninjectKernel.Bind<IRepository<Manager>>().To<EfRepository<Manager>>().InRequestScope();
ninjectKernel.Bind<IRepository<ShoppingCenterSpace>>().To<EfRepository<ShoppingCenterSpace>>().InRequestScope();
ninjectKernel.Bind<IRepository<IndependentStoreSpace>>().To<EfRepository<IndependentStoreSpace>>().InRequestScope();
ninjectKernel.Bind<IRepository<Space>>().To<EfRepository<Space>>().InRequestScope();
// Services
ninjectKernel.Bind<IAuthenticationService<Admin>>().To<AdminFormsAuthenticationService>();
ninjectKernel.Bind<IAdminService>().To<AdminService>();
ninjectKernel.Bind<IManagerService>().To<ManagerService>();
ninjectKernel.Bind<IShoppingCenterSpaceService>().To<ShoppingCenterSpaceService>();
ninjectKernel.Bind<IIndependentStoreSpaceService>().To<IndependentStoreSpaceService>();
ninjectKernel.Bind<ISpaceService>().To<SpaceService>();
}
}
Upvotes: 0
Views: 1178
Reputation: 32725
InRequestScope
requires the Ninject.MVC3 extenison instead of an own ControllerFactory . Otherwise it behaves like InTransientScope
Upvotes: 1