Know the answer? Post it. Somebody with the same question will find it here.
Sachin SinghRank #66 · 26.2K reputationPosted Aug 24, 2021, 3:50 AM EDT
+3
See,
what are encapsulation and Abstraction? your answer will be abstraction means to show what is necessary and encapsulation means hiding the complexity. in short, Abstraction is a theoretical thing a thought process, which is achieved by encapsulation using access specifiers like private, protected, and internal.
similarly, IOC and DIP are principles, a thought process or you can think of them as theoretical things, which needs to be implemented.
Now, what exactly do DIP and IOC mean?
These principals say, invert the control of creating objects of other classes from the class, means, take the power of creating an object from a class and give this power to someone else.
For example, don't allow Employee class to create an object of Department Class, give this power to third class like Unity Container, and when it is needed it will provide the object to Employee class via Constructor of employee class.
public class Employee
{
private Department _Department;
public Employee(Department department)
{
this._department=department;
}
}
Now, at run time the Unity DI container (built-in, in ASP .NET Core) will create the object of the department class and provide it to your employee class.
In this way, you inverted the control using DI.
So, IOC and DIP are just principals (Theoretical) which are implemented using DI (practical).
+3
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Aug 24, 2021, 3:47 AM EDT
+1
There are many types of IOCs available and DI is one among them.
Inversion of control :- It’s a generic term and implemented in several ways (events, delegates etc).
Dependency Inversion Principle: In a bad design the high level class uses directly and depends heavily on the low level classes. In such a case if we want to change the design to direct the output to a new FileWriter class we have to make changes in the Copy class. (Let's assume that it is a very complex class, with a lot of logic and really hard to test). In order to avoid such problems we can introduce an abstraction layer between high level classes and low level classes. Since the high level modules contain the complex logic they should not depend on the low level modules so the new abstraction layer should not be created based on low level modules. Low level modules are to be created based on the abstraction layer.
According to this principle the way of designing a class structure is to start from high level modules to the low level modules:
High Level Classes --> Abstraction Layer --> Low Level Classes
Sachin SinghRank #66 · 26.2K reputationPosted Aug 24, 2021, 3:50 AM EDT
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Aug 24, 2021, 3:47 AM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts