0
votes

I am new to Dependency Injection and currently using Ninject as my DI. I have been playing with a ASP.Net MVC 5 application and have been reading "Pro ASP.NET MVC 5". I have followed the examples in the book on how to set up and use Ninject. Below is the code for my register services:

    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<ICustomerRepository>().To<CustomerRepository>();
        kernel.Bind<ICustomerUserDataRepository>().To<CustomerUserDataRepository>();
    } 

As for my controller I have below:

public class CustomerController : Controller
{
    private ICustomerRepository customerRepository;

    public CustomerController(ICustomerRepository customerRepo)
    {
        this.customerRepository = customerRepo;
    }

    // GET: Customer
    public ActionResult Index(int Id = 0)
    {
        Customer customer = customerRepository.GetCustomer(Id).First();

        return View(customer);
    }
}

This works fine as expected in the book. However, I have been playing around with some other code and wanted to further use Ninject to resolve some dependencies. For example, I am working on a custom helper for one of my Razor views. In the my helper code I have the following:

       using (IKernel kernel = new StandardKernel())
       {                
            ICustomerUserDataRepository customerUserDataRepo = kernel.Get<ICustomerUserDataRepository>();

When I run this, it complains that there is no binding defined for ICustomerUserDataRepository. I am assuming this is because I am using a new kernel with no defined bindings. I read that you need to load bindings in kernels through modules. So I made the following:

public class MyBindings : NinjectModule
{
    public override void Load()
    {
        Bind<ICustomerUserDataRepository>().To<CustomerUserDataRepository>();
    }
}

I then load the module when setting my kernel below:

using (IKernel kernel = new StandardKernel(new MyBindings()))
{                
       ICustomerUserDataRepository customerUserDataRepo = kernel.Get<ICustomerUserDataRepository>();

This however causes a "Error loading Ninject component ICache" error message when I execute the application. I would appreciate some help on what I am doing wrong and what I am not understanding. I read that multiple defined kernels can cause this error. Am I not suppose to be using a new kernel in my helper method since one is already being used and binded under RegisterServices()? If so, am I suppose to access that existing kernel in my helper method? Or am I on the right track and need a new kernel loading the specific bindings in my module? Thank you.

1

1 Answers

0
votes

Am I not suppose to be using a new kernel in my helper method since one is already being used and binded under RegisterServices()?

correct. you only want one kernel or Composition Root per application. I would suggest that instead of trying to access dependencies in a helper method, that you would create a viewmodel in your Controller (that has access to the dependency) and then pass the viewmodel into your helper method.