Dependency injection and IOC Pattern
When you have a problem while coding your project or projecting a new solution, you should ask to yourself:
is a know issue?
If the response is true, then probably exists a know common solution!
This solution go under the name of Pattern.
Before you go further, it is important to understand the difference between principle and pattern.
Design Principle
Design principles provide high level guidelines to design better software applications. They do not provide implementation guidelines and are not bound to any programming language. The SOLID (SRP, OCP, LSP, ISP, DIP) principles are one of the most popular sets of design principles.
For example, the Single Responsibility Principle (SRP) suggests that a class should have only one reason to change. This is a high-level statement which we can keep in mind while designing or creating classes for our application. SRP does not provide specific implementation steps but it’s up to you how you implement SRP in your application.
Design Pattern
Design Pattern provides low-level solutions related to implementation, of commonly occurring object-oriented problems. In other words, design pattern suggests a specific implementation for the specific object-oriented programming problem. For example, if you want to create a class that can only have one object at a time, then you can use the Singleton design pattern which suggests the best way to create a class that can only have one object.
Inversion of Control
When you write some code, sure you match in the scenario where you have to manage many object creation behaviors. What happens if, for some requirements, you need to change the behavior of all your object constructors?
You have to deep refactor all your code, spending much effort and losing time and maybe a bit of brain XD.
To resolve this, you can centralize the control into one single point, a class, where you can set the behaviors for your class instances. In this case, control refers to any additional responsibilities a class has, other than its main responsibility, such as control over the flow of an application, or control over the dependent object creation and binding (Remember SRP — Single Responsibility Principle?). If you want to do TDD (Test Driven Development), then you must use the IoC principle, without which TDD is not possible.
Dependency Inversion Principle
The DIP principle also helps in achieving loose coupling between classes. It is highly recommended to use DIP and IoC together in order to achieve loose coupling.
DIP suggests that high-level modules should not depend on low level modules. Both should depend on abstraction.
The DIP principle was invented by Robert Martin (a.k.a. Uncle Bob). He is a founder of the SOLID principles.
Dependency Injection
Dependency Injection (DI) is a design pattern which implements the IoC principle to invert the creation of dependent objects.
There are 3 type of dependecy injection:
Constructor injection: the object dependencies are injected in the constructor.
Property injection: the object dependencies are injected into specified public proporties.
Method injection: the object dependencies are injected into specified public methods.
The first two are the most used.
Using this pattern you can build a low coupling project: a class must have references only to the contracts/interfaces (abstraction) of the dependency objects; in this way, if you change the implementation of a dependecy object, you’ll not have to refactor all code and the test for your new component can be successfull done with low impact.
Conclusion
This two patterns are often used togheter, using them in your code you’ll have a drastic coupling loss for your classes and can centralize the behavior for each instances.