-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix(dev): Correct errors in DI chapter code examples #13840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
a73bb1d
a57318c
0724b74
2577a1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,70 +1,161 @@ | ||||||||||
| ==================== | ||||||||||
| Dependency injection | ||||||||||
| ==================== | ||||||||||
|
|
||||||||||
| .. sectionauthor:: Bernhard Posselt <dev@bernhard-posselt.com> | ||||||||||
|
|
||||||||||
| The App Framework assembles the application by using a container based on the | ||||||||||
| software pattern `Dependency Injection <https://en.wikipedia.org/wiki/Dependency_injection>`_. | ||||||||||
| This makes the code easier to test and thus easier to maintain. | ||||||||||
|
|
||||||||||
| If you are unfamiliar with this pattern, watch the following video: | ||||||||||
| ================================= | ||||||||||
| Containers / Dependency Injection | ||||||||||
| ================================= | ||||||||||
|
|
||||||||||
| Introduction | ||||||||||
| ------------ | ||||||||||
|
|
||||||||||
| Modern software applications are composed of various components that need to interact | ||||||||||
| with one another. Traditionally, objects create their own dependencies internally, | ||||||||||
| which leads to tight coupling and makes code harder to test, maintain, and extend. | ||||||||||
| `Dependency injection (DI) <https://en.wikipedia.org/wiki/Dependency_injection>`_ is | ||||||||||
| a software design pattern that helps solve this problem by having dependencies provided | ||||||||||
| from the outside, rather than being constructed inside the object itself. | ||||||||||
|
|
||||||||||
| Dependency injection may sound like a big concept, but it’s really just about making | ||||||||||
| your code easier to work with and more flexible. Instead of each part of your app | ||||||||||
| creating the things it needs by itself, those “dependencies” are handed to it -- usually | ||||||||||
| by a special helper called a container. This means your classes don’t need to know how | ||||||||||
| to create their collaborators; they just need to know how to use them. | ||||||||||
|
|
||||||||||
| The App Framework in Nextcloud assembles applications using a container based on this | ||||||||||
| design pattern. This approach leads to more modular, testable, and maintainable code. | ||||||||||
|
|
||||||||||
| Using dependency injection is about more than just elegant code. When all apps follow | ||||||||||
| this pattern: | ||||||||||
|
|
||||||||||
| - It’s easier to test and upgrade both apps and the server, since dependencies can be | ||||||||||
| swapped out or mocked. | ||||||||||
| - Apps stay decoupled from internal server details, making it safer for Nextcloud to | ||||||||||
| evolve without breaking your app. | ||||||||||
| - Core features like autowiring, service discovery, and new APIs become available to all | ||||||||||
| apps without extra boilerplate. | ||||||||||
| - Memory and resource usage can be reduced. | ||||||||||
| - New services or APIs become easier to adopt as Nextcloud evolves. | ||||||||||
|
|
||||||||||
| By sharing a consistent approach to building and wiring up dependencies, everyone -- | ||||||||||
| core and app developers alike -- benefits from a more robust, secure, and future-proof | ||||||||||
| platform. | ||||||||||
|
|
||||||||||
| If you are unfamiliar with the DI design pattern, don't worry -- it's widely used in | ||||||||||
| modern frameworks, and you'll soon become comfortable with it. You can also watch the | ||||||||||
| following video introduction: | ||||||||||
|
|
||||||||||
| * `Google Clean Code Talks <https://www.youtube.com/watch?v=RlfLCWKxHJ0>`_ | ||||||||||
|
|
||||||||||
| .. _dependency-injection: | ||||||||||
|
|
||||||||||
| Dependency injection | ||||||||||
| -------------------- | ||||||||||
| Basic Pattern of Dependency Injection | ||||||||||
| ------------------------------------- | ||||||||||
|
|
||||||||||
| The essence of dependency injection is: **don't instantiate dependencies directly inside | ||||||||||
| your classes or methods, but instead pass them in as parameters**. This allows swapping | ||||||||||
| out dependencies (such as with mocks in unit tests), makes dependencies explicit, and | ||||||||||
| centralizes object creation logic. | ||||||||||
|
|
||||||||||
| Dependency Injection sounds pretty complicated but it just means: Don't put | ||||||||||
| new dependencies in your constructor or methods but pass them in. So this: | ||||||||||
| For example, consider the following pattern: | ||||||||||
|
|
||||||||||
| .. code-block:: php | ||||||||||
| :emphasize-lines: 10, 14 | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Without dependency injection: | ||||||||||
| */ | ||||||||||
|
|
||||||||||
| use OCP\IDBConnection; | ||||||||||
|
|
||||||||||
| // without dependency injection | ||||||||||
| class AuthorMapper { | ||||||||||
|
|
||||||||||
| // Define a property to store the dependency | ||||||||||
| private IDBConnection $db; | ||||||||||
|
|
||||||||||
| public function __construct() { | ||||||||||
| // The dependency is instantiated within the class | ||||||||||
| $this->db = new Db(); | ||||||||||
|
Comment on lines
+73
to
74
|
||||||||||
| // The dependency is instantiated within the class | |
| $this->db = new Db(); | |
| // The dependency is resolved within the class | |
| $this->db = \OC::$server->get(IDBConnection::class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
\OC::$server is deprecated @copilot.
Copilot
AI
Apr 29, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code examples were updated to use IRequest::class, but the following explanatory bullet list still refers to Request being queried. Please update the surrounding prose to match the IRequest type used in the examples for consistency.
Uh oh!
There was an error while loading. Please reload this page.