Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor with parameter in Controller - MVC

I have read few articles on IOC and Unity and got my self confused :(

So going back to basics can some one tel me what the below code does?

private IStudent _student;
public HomeController(IStudent student)
{
     _student= student;
}

public interface IStudent 
{
     // Some method
}

Itz Basic but am trying to understand from a layman view. What exactly the above code does?

like image 425
user2067567 Avatar asked Mar 11 '13 06:03

user2067567


3 Answers

HomeController has a dependency on Student because it delegates some responsibility to Student class.

One way to implement is:

public HomeController()
{
    private Student _student;
    public HomeController()
    {
        _student = new Student();
    }
}
public class Student 
{
    // Some method
}

but then HomeController has a hard dependency on Student class. What if you wanted to use some other implementation of Student (e.g. wanted to mock Student while unit testing your HomeController). You will have to modify the Student class or HomeController class (or use some other not-so-good option). This means your HomeController is tightly-coupled to Student class.

The other way is the code you have posted:

public class HomeController
{
    private IStudent _student;
    public HomeController(IStudent student)
    {
        _student = student;
    }
}
public interface IStudent
{
    // Some method
}
public class Student : IStudent
{
    // Implementation of some method
}

Here, you can pass on any implementation of IStudent i.e. in your unit tests you can pass the mock object of IStudent, in your actual code you will pass object of Student class. So you HomeController is now dependent on the IStudent interface (abstraction) rather than on the Student class (an implementation). This is inline with the OOP principles:

Program to interfaces, not implementations.

Depend on abstractions. Do not depend on concrete classes.

Also, it now has a soft dependency. It's no longer tightly-coupled to the Student class. It's loosely-coupled. Now, generally you don't need to worry about which implementation of IStudent you should pass while instantiating your HomeController. That's something the Depedency Injection Container (Unity in your case) will take care of, as long as you register correct interface and classes with it.

_container.Register<IStudent, Student>();

So when a new instance of HomeController is required, the container will identify that an instance of IStudent is required. So it will instantiate the registered instance of IStudent and pass it as paramter while instantiating HomeController class.

Also, note that what you are referring to is 'Dependency Injection' (which is one specific form of IoC). There are other forms of IoC (e.g. callbacks, Observer pattern, etc.).

EDIT: Don't forget to read the popular article on DI.

like image 134
publicgk Avatar answered Nov 17 '22 14:11

publicgk


In IoC you need to register the interface and a class which is implementing the interface. So once you register then whenever you have signature like above IoC will automatically create an instance of IStudent implemented class and inject it to the object while initializing the controller. It saves the time and efforts to declare the members. In above case you just have one to declare, but it could be more in numbers and those all could need few more instances to pass to the controller. Once we register those all correctly IoC do its work thereafter. Infact we can decide the Scope/Lifetime of the injected members. It can be PerInstance/Per Request/ Or Singleton.

There are saveral IoC frameworks available its up to you which one you want to go with.

like image 22
K D Avatar answered Nov 17 '22 14:11

K D


In general it is called injecting the dependencies of a class , think about a class or to be precise a GOD class which handles all the stuff like ( Validating user input , coordinating with Database , generating HTML output etc) so you keep all your code in single place or you can say you develop all of your software using single class , isn't that good?

Answer depends upon the way you organize the things , do you think , organizing things where it belongs is beneficial than you will see the problem in the above GOD class.

So in terms of OOPS , A single class shd have a single reason to be changed but it is required to get the work done than it shd take help from the services.

And your HomeController is doing the same thing since it does not want to be overworked , it has asked Student object to handle student.

like image 21
TalentTuner Avatar answered Nov 17 '22 12:11

TalentTuner