Dagger 2: is it possible to customize the lazy creation of default?


In General, I began to study/implement Dagger 2 and made a blunder: did everything according to normal (I understand that they are created from the entire module), and I need that they are all created on demand. Is it possible to configure so Dagger worked by default? And then redo a hundred places have...

Thanks in advance!
July 2nd 19 at 18:19
2 answers
July 2nd 19 at 18:21
In General, I do not understand correctly:

Dagger creates dependencies on demand (what I wanted). Lazy envy need when the dependence should be implemented without its creation.
July 2nd 19 at 18:23
It is impossible from the point of view of the language itself. If you have a normal field of type object - or null if the injection has not worked, or null if the injection has worked. It is impossible in principle to make the field was not null and lazily initialized when you first appeal to him
That is, the answer to your question is "no". Dagger and nothing to do with it :)
Another question arises: do you really need so they are all created on demand? You feel a noticeable drawdown performance when you initialize the object graph? What is really the problem of lazy initialization?
Might not exactly put it: on demand - not at the place of use, and means at the place of injection. Dagger generates an inject method. Nobody forbids him to check in this method whether implemented already created the object or not (and if not - create it). Here the only constraint is that these methods check/create dlzhny to be synchronized, but this is IMHO not a problem.

As for the problems - the question is more theoretical. Yes, I have one dependency which is long inicializirati, but it's not a problem to redo it. Just a container that yuzaetsya on the backend knows it's all out of the box, just switching paramtre config\ annotation. It is strange that Dagger is not able such. - Freeman.Stark80 commented on July 2nd 19 at 18:26
That is, your problem is to divide a single object instance among multiple classes that depend on it, not to re-create. For this Dagger 2 has a wonderful mechanism called Scopes. It allows you to specify the lifetime of an object, to do "local" singletons, which will be common to all dependent objects. Maybe you had this in mind? Or I again do not understand? - alvina.Herman commented on July 2nd 19 at 18:29

Find more questions by tags Android