8000 refactor(injection_types): refactoring on disadvantages · symfony/symfony-docs@af46b4f · GitHub
[go: up one dir, main page]

Skip to content

Commit af46b4f

Browse files
committed
refactor(injection_types): refactoring on disadvantages
Refactoring on disadvantages for setter and immutable setter.
1 parent 577760f commit af46b4f

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

service_container/injection_types.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,11 @@ so, here's the advantages of immutable-setters:
191191
* Your service becomes immutable, as the container will return a new object,
192192
the initial service stays clean and unchanged.
193193

194-
* You can easily change the injected service as long as it respect the interface/type
195-
asked by the initial service.
194+
* This type of injection works well with traits as the service can be composed,
195+
this way, adapting the service to your application requirements is easier.
196+
197+
* The setter can be called multiple times, this way, adding a dependency to a collection
198+
becomes easier and allows you to add a variable number of dependencies.
196199

197200
The disadvantages are:
198201

@@ -204,8 +207,8 @@ The disadvantages are:
204207
you can found that adding docblock for a single method isn't adapted or
205208
link your code to the container.
206209

207-
* As this approach force the container to create a new object
208-
once the method is called, you can found hard to debug and test your code.
210+
* Unless the service is declared lazy, it is incompatible with services
211+
that reference each other in what are called circular loops
209212

210213
Setter Injection
211214
----------------
@@ -278,6 +281,9 @@ This time the advantages are:
278281
the method adds the dependency to a collection. You can then have a variable
279282
number of dependencies.
280283

284+
* Like the immutable-setter one, this type of injection works well with
285+
traits and allows you to compose your service.
286+
281287
The disadvantages of setter injection are:
282288

283289
* The setter can be called more than just at the time of construction so

0 commit comments

Comments
 (0)
0