babajaj
babajaj

Reputation: 228

Creating objects in Dagger

I'm trying to learn Dagger, and I don't understand how I can actually get an instance of a class without writing a component for it.

In Guice, this would be as simple as:

final InterfaceA a = injector.getInstance(InterfaceA.class);

But from I understand, in Dagger I could create a component for InterfaceA so I could then call DaggerInterfaceA.a().

I'm wondering then, if there is maybe there a way to get an instance of an object without needing to make a component every time?

Upvotes: 0

Views: 451

Answers (1)

Ruokki
Ruokki

Reputation: 957

Guice use reflection so he is able to build object at runtime and the graph dependency is done during the runtime.

Dagger use Code generation so the build of the graph dependency is made when you build your application. This code generation will be done only for cleary declared component otherwise dagger would have to generate code for all class of your project (or even your classpath). So that would be totally inefficient (or impossible for complexe object)

Upvotes: 1

Related Questions