AddBindings in Ninject and null repository in Controller class

588 views Asked by At

In my NinjectDependencyresolver I have this:

public NinjectDependencyResolver(IKernel kernelParam)
{
    this.kernel = kernelParam;
    AddBindings();
}

private void AddBindings()
{
    kernel.Bind<IProductsRepository>().To<EFProductRepository>();
}

and then in my controller class I have this:

public class ProductController : Controller
{
    private IProductsRepository repository;
    public int PageSize = 4;

    public ProductController()
    {

    }

    public ProductController(IProductsRepository productRepository)
    {
        this.repository = productRepository;
    }

The problem is that the repository is null Also If I add a break point to the AddBinsings() method, it doesn't get hit before going to the controller, controller gets hit but AddBindings() does not. So does it mean it is a problem with my Ninject?

ALSO: I added the parameter less constructor of ProductController after getting this error:

No parameterless constructor defined for this object

I don't think I need that constructor, but if I remove it I get that error.

2

There are 2 answers

0
ConfusedSleepyDeveloper On BEST ANSWER

Should have also called it in Gloabal.ashx.cs file

3
jhilden On

I would start by removing the constructor to ProductController that has no parameters. This will force ninject to use the constructor with IProductsRepository.

As for the binding part, we have the binding taking place inside the NinjectWebCommon.cs file. Here is our sample:

   public static class NinjectWebCommon
    {
        private static readonly Bootstrapper Bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start()
        {
            DynamicModuleUtility.RegisterModule(typeof (OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof (NinjectHttpModule));
            Bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            Bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {   
            var kernel = new StandardKernel(new VBNinjectModule());
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);

            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            BindWebSpecificServices(kernel);

            GlobalConfiguration.Configuration.DependencyResolver = new VBNinjectDependencyResolver(kernel);
            return kernel;
        }

        public static void BindWebSpecificServices(IKernel kernel)
        {
            kernel.Bind<IUserHelper>()
                  .To<UserHelper>()
                  .InRequestScope();

            kernel.Bind<IRoleWebService>()
                .To<RoleWebService>()
                .InRequestScope();
}