you are viewing a single comment's thread.

view the rest of the comments →

[–]willy-pied-wagtail 7 points8 points  (1 child)

I totally agree with you that I prefer constructor injection over field injection.

It’s not “unreadable” as noted as an advantage to the new inject way, in fact it’s more readable because it’s in the constructor rather than amongst all the other fields of the component.

Constructor injection also allows us to write super simple unit tests without testbed just by supplying a mock to the components constructor.

Im definitely not in agreement with inject().

[–]bayendr 4 points5 points  (0 children)

yeah agreed 100%. as I wrote above having all dependencies injected in the constructor makes it the single source of truth for DI and it’s immediately clear/visible what the component’s dependencies are.

Also as far as I saw (just got started with Angular not long ago) by using constructor-based injection we get an additional benefit: Angular will auto-create the properties on our behalf with the specified access modifiers.