Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DI][Proposal] Annotation-based autowiring #25810

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

Closed
andrewtch opened this issue Jan 16, 2018 · 9 comments
Closed

[DI][Proposal] Annotation-based autowiring #25810

andrewtch opened this issue Jan 16, 2018 · 9 comments

Comments

@andrewtch
Copy link
Contributor

andrewtch commented Jan 16, 2018

Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? yes
Symfony version 4.0

I propose to introduce @Autowire annotation that will inject services into private / protected properties (the same way as Doctrine does with entities) instead of passing them to constructor. This can work like so:

class Service
{
    /**
     * @Autowire
     * @var Symfony\Component\Routing\RouterInterface
     */
    protected $router;
}

This would mimic Spring's @Autowired annotation and allow simpler DI if necessary, especially when inheriting services (on a small performance cost of reflection call, which is to be measured). Of course, container - injection should stay.

Was this behaviour discussed before?

See: http://www.baeldung.com/spring-autowire

@stof
Copy link
Member

stof commented Jan 16, 2018

I'm -1 for providing direct autowiring into private properties. This would mean that your class is unusable in a normal way.

and btw, injecting a service into a Doctrine entity is a bad signal about the architecture...

@andrewtch
Copy link
Contributor Author

andrewtch commented Jan 16, 2018

@stof - sorry, my bad, I meant "inject the same way as doctrine does". Entities are unrelated here, sorry for the confusion.

Class with 3-4 constructor arguments is also unusable in "normal way".

See http://www.baeldung.com/spring-autowire . Description updated.

@stof
Copy link
Member

stof commented Jan 16, 2018

@andrewtch it is not unusable. It is unfriendly. this is not the same.

Forcing to use reflection to use a class means that your class is restricted to be used by systems bypassing the API of your class (btw, it also means that such class is almost untestable as you would not use the DIC to initialize it in tests...).

And btw, this cause another sort of issue: PHP properties cannot be typed, so there is no reliable way to know the type for autowiring (the phpdoc is hard to use, because we would have to resolve class names in them manually, forcing to parse the source code ourselves to know use statements, etc... Phpdocumentor has a whole library dedicated to parsing phpdoc, with lots of the logic related to this resolution of FQCN and terrible performance)

@andrewtch
Copy link
Contributor Author

Good point. I will dig in more on Spring's approach to autowiring and tests.

PHPDocumentor parsing is indeed slow, noticed it on Swagger documentation.

@dunglas
Copy link
Member

dunglas commented Jan 16, 2018

When we designed the 2.8 version of the autowiring mechanism, we tried hard to avoid such solution because of the arguments mentioned by @stof, and because the classes should know nothing about the DIC (and basically the way they are initialized). The only exception (and I'm not fond of it is @required for setter injection).

I'm 👎 to add this too. It will be slower and encourage bad design.

@dunglas
Copy link
Member

dunglas commented Jan 16, 2018

Regarding properties's types and PHPDoc parsing, we can use the PropertyInfo component for that (it has a cache layer for performance). But as said previously I wouldn't go this way.

@linaori
Copy link
Contributor

linaori commented Jan 17, 2018

Similar proposals have been rejected for said reasons: #22467 & #21103, possibly some more.

@chalasr
Copy link
Member

chalasr commented Jan 17, 2018

👎 as well, closing.

@chalasr chalasr closed this as completed Jan 17, 2018
@dunglas
Copy link
Member

dunglas commented Jan 17, 2018

Anyway, it can be done as a community bundle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants