Symfony2 Lazy Services When to use?
Asked Answered
M

1

7

I have a question regarding symfony2 lazy services. When we should use lazy services, and when we should avoid them? Is there any overhead if we use lazy services?

Metonym answered 7/1, 2015 at 7:52 Comment(0)
K
5

From the documentation:

In some cases, you may want to inject a service that is a bit heavy to instantiate, but is not always used inside your object. For example, imagine you have a NewsletterManager and you inject a mailer service into it. Only a few methods on your NewsletterManager actually use the mailer, but even when you don't need it, a mailer service is always instantiated in order to construct your NewsletterManager.

Configuring lazy services is one answer to this. With a lazy service, a "proxy" of the mailer service is actually injected. It looks and acts just like the mailer, except that the mailer isn't actually instantiated until you interact with the proxy in some way.

Yes, there is some overhead. But it is minimal. You should avoid using lazy services when you don't need them. (Easy as that).

Example:

If your service A has 3 methods and depends on B and C. If you know that B is used in all 3 methods and C i only used in one method then you may consider declaring C as lazy. You should declare it has lazy if C is a heavy service. In this example there will not be any benefit of declaring B as lazy... so don't... =)

Kairouan answered 7/1, 2015 at 8:42 Comment(6)
Is it correct if I make all of my services lazy? What are the drawbacks if I have a lazy service instead of normal service?Metonym
As far as I understand there is no but the overhead. Doctrine lazyloads all its entities by default. The different between models and services is that the services should not depend (have a relation to) that it's not using. So, if you feel that you have lots of dependencies that you are not using, then your service is too big. It is only in rare cases you need this feature. Refactoring should be your first choice.Kairouan
My Question was in general and I just wanted to know that If I have 10-15 services in my application few may be dependent on other services and few are not. How symfony2 treats them. Are they going to instantiate from the beginning or they are going to be instantiated when I invoke them(by calling $this->get('service_name')). Does making them lazy will help in making system fast?Metonym
Okey, I understand your question now. When you call $this->get('service_name')) then the service 'service_name' and all its dependencies will be instantiated. Dependencies declared as lazy will be instantiated at first use (When you actually want something from that lazy service). Eg $this->get('service_name'))->getMailerService->send() All your other non use services will not be instantiated.Kairouan
There actually is an overhead to using lazy services. Basically, a proxy object is created. So don't make all your services lazy. As @Tobias has said, only declare a service lazy if it's heavy to instantiate and you don't know if you will actually need it. Injecting the mailer into a listener which may or may not actually need to mail something depending on the specific request is the classic use case.Lugar
I would also lazyload some twig extension services, as you won't probably use them all at once, and ALL twig extension services are instantiated as soon as you require the templating engine.Flatiron

© 2022 - 2024 — McMap. All rights reserved.