# DI / IoC

## IoC

**inversion of control** (**IoC**) is a programming principle. IoC inverts the [flow of control](https://en.wikipedia.org/wiki/Control_flow) as compared to traditional control flow. In IoC, custom-written portions of a [computer program](https://en.wikipedia.org/wiki/Computer_program) receive the flow of control from a generic [framework](https://en.wikipedia.org/wiki/Software_framework). A [software architecture](https://en.wikipedia.org/wiki/Software_architecture) with this design inverts control as compared to traditional [procedural programming](https://en.wikipedia.org/wiki/Procedural_programming): in traditional programming, the custom code that expresses the purpose of the program [calls](https://en.wikipedia.org/wiki/Function_call#Main_concepts) 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 pattern](https://en.wikipedia.org/wiki/Software_design_pattern) in which an [object](https://en.wikipedia.org/wiki/Object_\(computer_science\)) or [function](https://en.wikipedia.org/wiki/Subroutine) receives other objects or functions that it depends on. A form of [inversion of control](https://en.wikipedia.org/wiki/Inversion_of_control), dependency injection aims to [separate the concerns](https://en.wikipedia.org/wiki/Separation_of_concerns) of constructing objects and using them, leading to [loosely](https://en.wikipedia.org/wiki/Loose_coupling) [coupled](https://en.wikipedia.org/wiki/Coupling_\(computer_programming\)) programs.[\[1\]](https://en.wikipedia.org/wiki/Dependency_injection#cite_note-1)[\[2\]](https://en.wikipedia.org/wiki/Dependency_injection#cite_note-MarkSeeman2011P4-2)[\[3\]](https://en.wikipedia.org/wiki/Dependency_injection#cite_note-3) The pattern ensures that an object or function which wants to use a given [service](https://en.wikipedia.org/wiki/Service_\(systems_architecture\)) should not have to know how to construct those services. Instead, the receiving '[client](https://en.wikipedia.org/wiki/Client_\(computing\))' (object or function) is provided with its dependencies by external code (an 'injector'), which it is not aware of.[\[4\]](https://en.wikipedia.org/wiki/Dependency_injection#cite_note-HollywoodPrinciple.c2-4) Dependency injection helps by making implicit dependencies explicit and helps solve the following problems:[\[5\]](https://en.wikipedia.org/wiki/Dependency_injection#cite_note-5)

* How can a [class](https://en.wikipedia.org/wiki/Class_\(computer_programming\)) 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?


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zer0-2.gitbook.io/type-chef-di/other/di-ioc.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
