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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With