Home  >  Article  >  What is dependency injection

What is dependency injection

清浅
清浅Original
2019-04-01 16:48:017072browse

Dependency injection is a design pattern that implements inversion of control and solves dependency problems. It has three types: constructor injection, property injection and method injection. It has benefits such as reducing dependencies and enhancing the reusability of components

Dependency injection (DI) is a design pattern that eliminates dependencies between programming codes, so applications can be easily managed and tested program. Next, in the article, we will introduce in detail what dependency injection is, which has a certain reference effect. I hope it will be helpful to everyone

What is dependency injection

[Recommended courses:Spring Tutorial

Dependency Injection:

In programming, dependency injection It is a design pattern that implements inversion of control and is used to solve dependency problems. A dependency refers to an object that can be exploited. Dependency injection is passing dependencies to dependent objects to be used. The service will become part of the client's state and pass the service to the client without allowing the client to create or find the service. Dependency injection makes our programming code loosely coupled and easy to manage

What is dependency injection

Types of dependency injection:

The injected class will serve (Dependencies) Injection into the client (dependencies) can be divided into the following forms

(1) Constructor injection: In constructor injection, the injector provides services (dependencies) through the client class constructor ).

(2) Property injection: In property injection (also known as Setter injection), the injector provides dependencies through public properties of the client class.

(3) Method injection: In this type of injection, the client class implements an interface that declares methods to provide dependencies, and the injector uses this interface to provide dependencies on the client class sex.

Benefits of dependency injection:

(1) Reduce dependencies

Dependency injection can eliminate or reduce the inconsistencies between components Necessary dependencies. To reduce the impact of component changes Component

(2) Enhance reusability

Reducing component dependencies can enhance component Reusability. If a different implementation of an interface is required in a different context, or just a different configuration of the same implementation, the component can be configured to use that implementation. No code changes required.

(3) Increase the testability of the code

Dependency injection also increases the testability of the component. When dependencies can be injected into a component, it means that mock implementations of those dependencies can be injected. Mock objects are used for testing as a substitute for the actual implementation, and the behavior of the mock objects can be configured

(4) Enhance the readability of the code

Dependency injection can convert dependencies The item is moved to the component's interface. Makes it easier to see which components have dependencies, making the code more readable.

(5)Reduce dependency bearing

Dependency bearing can create a lot of "noise" in the code, making it difficult to read and maintain , and makes the component harder to test. Dependency injection can reduce dependency bearing and the use of static singletons, and can perfectly connect components together

Summary: The above is the entire content of this article, I hope it will be helpful to everyone

The above is the detailed content of What is dependency injection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn