1

I am learning ASP.Net MVC. I am referring Adam Freeman's Pro Asp.Net MVC 4 book. I came across a topic name Building Loosely Couple App in that Dependency Injection I was not getting what actually it is, so I tried to write his example in short cut.

I created an Interface IReservation as below

public interface IReservation
    {
        string getName();
    }

and a class that implements it as below

public class Reservation:IReservation
    {

        public string getName()
        {
            return "Wooww... It worked :D ";
        }
    }

Then I passed interface's object to my controller constructor same as in book. I have controller code as below

public class HomeController : Controller
    {
        IReservation res;
        public HomeController(IReservation reservation)
        {
            res = reservation;
        }
        public HomeController() { }
        public ActionResult Index()
        {
            string me = res.getName();
            return View();
        }
}

While debugging, I am getting NullReferenceException on line string me = res.getName();. I believe its because I am not creating it's instance using new keyword but I am not sure. Am I right here, if yes then how this example can be corrected? I have interface and implementing class in Models folder.

Imad
  • 7,126
  • 12
  • 55
  • 112
  • 1
    Dear downvoter.! downvote me for lack of DI knowldge but atleast learn DI and point me to original of this duplicate :P :D If you cant find that, keep your downvote alive but remove your reference. – Imad Nov 16 '14 at 14:04
  • I suggest you read the book [Dependency Injection in .NET](http://www.manning.com/seemann/) by Mark Seemann - it is a great resource for learning about DI. – NightOwl888 Nov 16 '14 at 14:31

1 Answers1

3

You should remove the default constructor of your controller which is never injecting any IReservation instance:

public HomeController() { }

Now your controller definition becomes:

public class HomeController : Controller
{
    private readonly IReservation reservation;
    public HomeController(IReservation reservation)
    {
        this.reservation = reservation;
    }

    public ActionResult Index()
    {
        string me = this.reservation.getName();
        return View();
    }
}

All that's left now is to bootstrap and configure your favorite DI framework so that injects the proper instance of IReservation in your controller.

For example if you are using Ninject.MVC in your bootstrap:

kernel.Bind<IReservation>.To<Reservation>();

If you are using some other DI container you should obviously adapt the syntax to it and register the proper instance when bootstrapping your application.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I had no default constructor initially, but it was throwing `No parameterless constructor defined for this object.` at runtime. So I used that. – Imad Nov 16 '14 at 13:31
  • If it is throwing this error then you probably didn't yet pickup a Dependency Injection framework and configured it properly. Which DI framework do you use in your MVC application? How did you configure it? You need to specify in your DI bootstrap code that `IReservation` instance should resolve to the `Reservation` class. Of course depending on the DI framework you choose the way to configure this will be different. You might consider reading the documentation of the DI framework of your choice. – Darin Dimitrov Nov 16 '14 at 13:31
  • I haven't done anything like this. As it was not mentioned in book yet. I think I have to learn it from outside or keep moving until I get this topic in detail somewhere. :). It will be goof if you share some important link :) – Imad Nov 16 '14 at 13:34
  • 2
    If you haven't choose a DI framework yet, then it's normal that you are getting such errors. The purpose of the DI framework is to have configured specific implementations of given interfaces that will be injected in your controllers. Here's a begginer's getting started tutorial for DI with Unity in ASP.NET MVC that you might go through: http://www.asp.net/mvc/overview/older-versions/hands-on-labs/aspnet-mvc-4-dependency-injection but of course you could use any DI framework available you want. – Darin Dimitrov Nov 16 '14 at 13:38