Sobes.tech
Junior — Middle

Selgitage, mis on komponendid Dagger'i raamatukogus ja kuidas neid kasutatakse sõltuvuste süstimiseks

sobes.tech AI

Vastus AI-lt

In Dagger, a component is an interface that connects dependency providers (modules) and consumers (classes where dependencies are injected). Components define which dependencies are available for injection and manage their lifecycle.

Using components allows:

  • To determine which objects can be obtained via dependency injection.
  • To connect modules that provide specific implementations.
  • To manage the scope of visibility of dependencies (e.g., singleton).

Example:

@Component(modules = NetworkModule.class)
public interface AppComponent {
    void inject(MainActivity activity);
}

Here, AppComponent connects the NetworkModule and allows injecting dependencies into MainActivity.