0

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 Answers2

1

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();
}
jhilden
  • 12,207
  • 5
  • 53
  • 76
  • Yeah the Parameterless constructor wasn't originally in my code, I had to add it because of this error: "No parameterless constructor defined for this object" I took it now, now getting that error. – ConfusedSleepyDeveloper Nov 17 '14 at 22:30
  • "No parameterless constructor defined for this object" means that it's not binding ninject to IProductsRepository. Take a look at my NinjectWebCommon and compare it to your own. – jhilden Nov 17 '14 at 22:38
  • oh ok, so I added kernel.Bind().To(); to the CreateKernel() method, looks like you are binding them there too. – ConfusedSleepyDeveloper Nov 17 '14 at 22:46
0

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