Ninject does not inject and throw unnecessary reference exceptions

I'm new to Ninject, so I'm sure this is what I'm doing wrong, I'm just not sure what. I am using Ninject and Ninject.MVC3 in my MVC3 web application. Here is an example of what I'm trying to do.

I am using the repository template:

public interface IRepository<T>
{
    T Get(object id);
    IList<T> GetAll();
    void Add(T value);
    void Update(T value);
    void Delete(T value);
}

For a specific type:

public Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }

    public Customer()
    {
    }
}

Now I have 2 separate repositories, a cached version that needs to be injected into the database repository:

public CachedCustomerRepository : IRepository<Customer>
{
    private IRepository<Customer> _repository;

    public Customer Get(object id)
    {
        Customer cust = new Customer();
        IList<Customer> custs = GetAll();
        if (custs != null && custs.Count > 0)
            cust = custs.FirstOrDefault(c => c.Id == int.Parse(id.ToString()));

        return cust;
    }

    public IList<Customer> GetAll()
    {
        IList<Customer> custs = HttpRuntime.Cache["Customers"] as IList<Customer>;
        if (custs == null)
        {
            custs = _repository.GetAll();
            if (custs != null && custs.Count() > 0)
            {
                double timeout = 600000d;
                HttpRuntime.Cache.Insert("Customers", custs, null, DateTime.UtcNow.AddMilliseconds(timeout), System.Web.Caching.Cache.NoSlidingExpiration);
            }
            else
            {
                throw new NullReferenceException();
            }
        }

        return custs;
    }

    public void Add(Customer value)
    {
        throw new NotImplementedException();
    }

    public void Update(Customer value)
    {
        throw new NotImplementedException();
    }

    public void Delete(Customer value)
    {
        throw new NotImplementedException();
    }

    public CachedCustomerRepository()
    {
    }

    [Inject]
    public CachedCustomerRepository(IRepository<Customer> repository)
    {
        _repository = repository;
    }
}

public class CustomerRepository : IRepository<Customer>
{   
    public Customer Get(object id)
    {
        Customer cust = new Customer();
        IList<Customer> custs = GetAll();
        if (custs != null && custs.Count > 0)
            cust = custs.FirstOrDefault(c => c.Id == int.Parse(id.ToString()));

        return cust;
    }

    public IList<Customer> GetAll()
    {
        //Customer retrieval code
    }

    public void Add(Customer value)
    {
        throw new NotImplementedException();
    }

    public void Update(Customer value)
    {
        throw new NotImplementedException();
    }

    public void Delete(Customer value)
    {
        throw new NotImplementedException();
    }

    public CachedCustomerRepository()
    {
    }
}

I installed NinjectModule as follows:

public class ServiceModule : NinjectModule
{
    public override void Load()
    {
        Bind<IRepository<Customer>>().To<CustomerRepository>();
    }
}

and I modified NinjectMVC3.cs in the AppStart folder to get the module when creating the kernel:

private static IKernel CreateKernel()
{
    var kernel = new StandardKernel(new ServiceModule());
    RegisterServices(kernel);
    return kernel;
}

In my controller, I use the following:

public ViewResult Index()
{
    IRepository<Customer> custRepo = new CachedCustomerRepository();
    return View(custRepo.GetAll());
}

He will explode on the line _repository.GetAll()in mine CachedCustomerRepository.

, , CreateKernel() , . , . : , , IRepository, - mvc3. -, Ninject, - Ninject.MVC3. -.

+5
5

-, . . -, , .

- :

public class SomeController
{
    private readonly IRepository<Customer> repo;

    public SomeController(IRepository<Customer> repo)
    {
        this.repo = repo;
    }

    public ViewResult Index()
    {
        return View(this.repo.GetAll());
    }
}

, Ninject ! Ninject IRepository<Customer> MVC. Ninject "", CustomerRepository .

, "CachedRepository"? , . , CustomerRepository, Ninject.

+3

Global.asax inherit NinjectHttpApplication?

0

, DependencyResolver.SetResolver(CreateKernel());, MVC, . , , .

0

IOC, CachedCustomerRepository . Ninject. , , "IRepository" CachedCustomerRepository MVC Controller. , , Ninject .

0

The problem is that when CachedCustomerRepositoryyou create actions in your method, you simply create an instance yourself, and you do not get Ninject to create it and subsequently make it dependent.

What you need to do is use the built-in constructor for the controller.

eg.

public class MyController : Controller
{
    public IRepository<Customer> CustomerRepo { get; protected set; }

    public MyController(IRepository<Customer> customerRepo)
    {
        CustomerRepo = customerRepo;
    }

    public ViewResult Index()
    {
        return View(CustomerRepo.GetAll());
    }
}

Your script is somewhat confusing, though, because you type IRepository<Customer>in IRepository<Customer>, which you need to insert in MyController.

0
source

All Articles