githubEdit

πŸ’‘DI / IoC

What is a DI / IoC?

IoC

inversion of control (IoC) is a programming principle. IoC inverts the flow of controlarrow-up-right as compared to traditional control flow. In IoC, custom-written portions of a computer programarrow-up-right receive the flow of control from a generic frameworkarrow-up-right. A software architecturearrow-up-right with this design inverts control as compared to traditional procedural programmingarrow-up-right: in traditional programming, the custom code that expresses the purpose of the program callsarrow-up-right into reusable libraries to take care of generic tasks, but with inversion of control, it is the framework that calls into the custom, or task-specific, code.

dependency injection is a design patternarrow-up-right in which an objectarrow-up-right or functionarrow-up-right receives other objects or functions that it depends on. A form of inversion of controlarrow-up-right, dependency injection aims to separate the concernsarrow-up-right of constructing objects and using them, leading to looselyarrow-up-right coupledarrow-up-right programs.[1]arrow-up-right[2]arrow-up-right[3]arrow-up-right The pattern ensures that an object or function which wants to use a given servicearrow-up-right should not have to know how to construct those services. Instead, the receiving 'clientarrow-up-right' (object or function) is provided with its dependencies by external code (an 'injector'), which it is not aware of.[4]arrow-up-right Dependency injection helps by making implicit dependencies explicit and helps solve the following problems:[5]arrow-up-right

  • How can a classarrow-up-right be independent from the creation of the objects it depends on?

  • How can an application, and the objects it uses support different configurations?

  • How can the behavior of a piece of code be changed without editing it directly?

Last updated