Note that there are some explanatory texts on larger screens.

plurals
  1. POASP MVC 2 Ninject
    primarykey
    data
    text
    <p>I'm trying to learn a bit about MVC and have come across a problem when using Ninject. I want to bind repositories but keep getting the 'Object reference not set to an instance of an object' error.</p> <p>I have created my NinjectControllerFactory:</p> <pre><code>public class NinjectControllerFactory : DefaultControllerFactory { // A Ninject "kernel" is the thing that can supply object instances private IKernel kernel = new StandardKernel(new SportsShopServices()); // ASP .NET MVC calls this to get the controller for each request protected override IController GetControllerInstance(RequestContext context, Type controllerType) { if (controllerType == null) return null; return (IController) kernel.Get(controllerType); } // Configure how abstract sevice types are mapped to concrete implementations private class SportsShopServices : NinjectModule { public override void Load() { Bind&lt;IProductRepository&gt;().To&lt;SqlProductsRepository&gt;() .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString); } } } </code></pre> <p>and my controller :</p> <pre><code>public class ProductsController : Controller { private IProductRepository productsRepository; // Constructor used with Ninject public ProductsController(IProductRepository _productsRepository) { this.productsRepository = _productsRepository; } public ViewResult List() { return View(productsRepository.Products.ToList()); } } </code></pre> <p>I have modified the Web.config file to provide the db connection string and the Global.asax file Application_Start() method to include:</p> <pre><code>ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory()); </code></pre> <p>I am working on an example from the PRO ASP .NET MVC 2 book but just can't get this work, been trying all day.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload