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

Skip to content

[DependencyInjection] Use lazy-loading ghost object proxies out of the box #46752

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

Merged
merged 1 commit into from
Jul 12, 2022

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Jun 23, 2022

Q A
Branch? 6.2
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #35345
License MIT
Doc PR -

This PR builds on #46751. It also replaces #46458.

Instead of using ProxyManager to make lazy services actually lazy, using LazyGhostObjectTrait from #46751 allows doing so out of the box - aka without the need to install any optional dependencies.

When a virtual proxy is required (typically when using the proxy tag), ProxyManager is still required (and the dep remains optional.)

But for most services, using LazyGhostObjectTrait just works \o/

@nicolas-grekas nicolas-grekas force-pushed the di-ghost-objects branch 9 times, most recently from df1a745 to 830a069 Compare July 1, 2022 14:30
@nicolas-grekas nicolas-grekas force-pushed the di-ghost-objects branch 6 times, most recently from 5a18093 to 5739354 Compare July 11, 2022 06:32
@nicolas-grekas
Copy link
Member Author

PR rebased and ready \o/

@nicolas-grekas nicolas-grekas requested a review from wouterj as a code owner July 12, 2022 13:52
@nicolas-grekas nicolas-grekas merged commit d095a50 into symfony:6.2 Jul 12, 2022
@nicolas-grekas nicolas-grekas deleted the di-ghost-objects branch July 12, 2022 15:57
nicolas-grekas added a commit to symfony/orm-pack that referenced this pull request Jul 12, 2022
fabpot added a commit that referenced this pull request Sep 3, 2022
…ng virtual proxies for non-ghostable lazy services (nicolas-grekas)

This PR was merged into the 6.2 branch.

Discussion
----------

[DependencyInjection][VarExporter] Generate lazy-loading virtual proxies for non-ghostable lazy services

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Since #46752 and #46751, we are able to make services lazy out of the box, except when 1. a service relies on an internal class 2. a service has the `proxy` tag or 3. a service's class is abstract (and the service uses a factory). In these situations, proxy-manager-bridge was required. This was an acceptable trade-off because this would be quite uncommon. But while working on Doctrine, I realized that we cannot use ghost objects when a factory is used. I described this for Doctrine in doctrine/orm#9896 but the situation can happen with any services constructed by a factory. This means we'd need proxy-manager-bridge anytime a factory is used on a lazy service. What was uncommon becomes quite common and the trade-off is not acceptable anymore. Thus this PR.

This PR adds a `LazyProxyTrait` and a `ProxyHelper` to build lazy loading virtual proxies at will. It then wires this new capability into the container. As a result proxy-manager-bridge is not needed anymore. We can deprecate it in another PR.

While the diff is quite big,  `LazyProxyTrait` has many similarities with `LazyGhostTrait` and both traits can be diffed to see where their behavior varies.

Excerpt from the [README](https://github.com/nicolas-grekas/symfony/blob/ve-inheritance-proxy/src/Symfony/Component/VarExporter/README.md) for the record:

>The component provides two lazy loading patterns: ghost objects and virtual proxies (see https://martinfowler.com/eaaCatalog/lazyLoad.html for reference.)
>
>Ghost objects work only on concrete and non-internal classes. In the generic case, they are not compatible with using factories in their initializer.
>
>Virtual proxies work on concrete, abstract or internal classes. They provide an API that looks like the actual objects and forward calls to them. They can cause identity problems because proxies might not be seen as equivalents to the actual objects.
>
>Because of this identity problem, ghost objects should be preferred when possible. Exceptions thrown by the ProxyHelper class can help decide when it can be used or not.
>
>Ghost objects and virtual proxies both provide implementations for the LazyObjectInterface which allows resetting them to their initial state or to forcibly initialize them when needed. Note that resetting a ghost object skips its read-only properties. You should use a virtual proxy to reset read-only properties.

Commits
-------

4862139 [DependencyInjection][VarExporter] Generate lazy proxies for non-ghostable lazy services out of the box
@fabpot fabpot mentioned this pull request Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants