You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
msgid"We've avoided unnecessary inheritance and used interface to reduce coupling. You can replace cache implementation without changing `CachedWidget` so it's becoming more stable."
114
+
msgid"We've avoided unnecessary inheritance and used `CacheInterface` in the `CacheWidget` to reduce coupling. You can replace cache implementation without changing `CachedWidget` so it's becoming more stable. The less edits are made to the code, the less chance of breaking it."
115
115
msgstr""
116
116
117
117
#. type: Plain text
118
118
#: ../src/guide/concept/di-container.md
119
-
msgid"The `CacheInterface` here is a dependency: an object another object depends on. The process of putting an instance of dependency into an object (`CachedWidget`) is called dependency injection. There are many ways to perform it:"
119
+
msgid"The `CacheInterface` here is a dependency: a contract our object needs to function. In other words, our object depends on the contract."
120
+
msgstr""
121
+
122
+
#. type: Plain text
123
+
#: ../src/guide/concept/di-container.md
124
+
msgid"The process of putting an instance of a contract into an object (`CachedWidget`) is called dependency injection. There are many ways to perform it:"
120
125
msgstr""
121
126
122
127
#. type: Bullet: '- '
@@ -198,12 +203,12 @@ msgstr ""
198
203
199
204
#. type: Bullet: '- '
200
205
#: ../src/guide/concept/di-container.md
201
-
msgid"Define how to instantiate such an API wrapper."
206
+
msgid"Define how to instantiate such common dependencies."
202
207
msgstr""
203
208
204
209
#. type: Bullet: '- '
205
210
#: ../src/guide/concept/di-container.md
206
-
msgid"Instantiate it when required and only once per request."
211
+
msgid"Instantiate them when required and only once per request."
207
212
msgstr""
208
213
209
214
#. type: Plain text
@@ -213,7 +218,7 @@ msgstr ""
213
218
214
219
#. type: Plain text
215
220
#: ../src/guide/concept/di-container.md
216
-
msgid"A dependency injection (DI) container is an object that knows how to instantiate and configure objects and all their dependent objects. [Martin Fowler's article](https://martinfowler.com/articles/injection.html) has well explained why DI container is useful. Here we will mainly explain the usage of the DI container provided by Yii."
221
+
msgid"A dependency injection (DI) container is an object that knows how to instantiate and configure objects and all objects they depend on."
217
222
msgstr""
218
223
219
224
#. type: Plain text
@@ -229,6 +234,24 @@ msgstr ""
229
234
msgid"Yii provides the DI container feature through the [yiisoft/di](https://github.com/yiisoft/di) package and [yiisoft/injector](https://github.com/yiisoft/injector) package."
230
235
msgstr""
231
236
237
+
#. type: Plain text
238
+
#: ../src/guide/concept/di-container.md
239
+
#, no-wrap
240
+
msgid""
241
+
"> [!NOTE]\n"
242
+
"> The container contains only shared instances. If you need a factory, use the dedicated\n"
msgid"That's basically it. You define a map of interfaces to classes and define how to configure them. When an interface is requested in constructor or elsewhere, container creates an instance of a class and configures it as per the configuration:"
msgid"There are extra methods of declaring dependency configuration."
358
+
msgstr""
359
+
360
+
#. type: Plain text
361
+
#: ../src/guide/concept/di-container.md
362
+
msgid"For simplest cases where there are no custom values needed and all the constructor dependencies could be obtained from a container, you can use a class name as a value."
msgid"If you have a dependency that has public properties, you can configure it as well."
399
+
msgstr""
400
+
401
+
#. type: Fenced code block (php)
402
+
#: ../src/guide/concept/di-container.md
403
+
#, no-wrap
404
+
msgid""
405
+
"final class NameProvider\n"
406
+
"{\n"
407
+
" public string $name;\n"
408
+
"}\n"
409
+
msgstr""
410
+
411
+
#. type: Plain text
412
+
#: ../src/guide/concept/di-container.md
413
+
msgid"Here's how to do it for the example above:"
414
+
msgstr""
415
+
416
+
#. type: Fenced code block (php)
417
+
#: ../src/guide/concept/di-container.md
418
+
#, no-wrap
419
+
msgid""
420
+
"NameProvider::class => [\n"
421
+
" 'class' => NameProvider::class, \n"
422
+
" '$name' => 'Alex',\n"
423
+
"],\n"
424
+
msgstr""
425
+
426
+
#. type: Plain text
427
+
#: ../src/guide/concept/di-container.md
428
+
msgid"In this example, you may notice `NameProvider` specified twice. The key is what you may request as dependency and the value is how to create it."
429
+
msgstr""
430
+
431
+
#. type: Plain text
432
+
#: ../src/guide/concept/di-container.md
433
+
msgid"If the configuration is tricky and requires some logic, a closure can be used:"
msgstr"Desacoplar Gestores de Eventos <span id=\"detaching-event-handlers\"></span>"
478
+
331
479
#. type: Plain text
332
480
#: ../src/guide/concept/di-container.md
333
481
msgid"Directly referencing a container in a class is a bad idea since the code becomes non-generic, coupled to the container interface and, what's worse, dependencies are becoming hidden. Because of that, Yii inverts the control by automatically injecting objects from a container in some constructors and methods based on method argument types."
0 commit comments