Closed degaray closed 8 years ago
In magento 2 all objects should be ready for use immediately after instantiation. Also most of our services are immutable, they don't change state after creation. Setter injection contradicts both these concepts, so is not supported.
Setter injection leads to broken encapsulation and temporal coupling. You should not use it in your objects. If you need different instances of one class configured with different dependencies, you can use argument configuration.
You can get more details in post by @alankent: http://alankent.me/2015/03/03/why-i-prefer-constructor-injection-over-setter-or-property-injection/
I agree on that, however, some already made third-party libraries use that principle and it can become handy for using such libraries.
It's always a good practice to use adapters for third-party system interfaces to allow implementation substitutions and made upgrades easier.
There should be a way to inject dependencies and have Magento2 call a setter. Some third party classes are done this way so it would be very good to have this feature. See Symfony3 injection for more idea.
Steps to reproduce
Expected result
Actual result