Simplest Example of Dagger1 – Dependency Injection

.When it comes to managing dependencies in our Java and Android code, Dagger comes first in our mind. dagger is fastest dependency injector for Java and Android. In this post, I’ll give you a complete understanding of Dependency, Dependency Injection in Java or Android. I’ll share Simplest Example of Dagger1 – Dependency Injection.

Square’s Dagger 1.x is deprecated in favor of Google’s Dagger 2. Please check the migration guide for help with the upgrade.

What is Dependency & Dependency Injection:

Dependency:

When it comes to dependency, we always think something is dependent on other thing. Dependency in case of programming can be defined as one object is dependent on other object or objects. For example, we have a Computer object which is dependent on Monitor, CPU, Keyboard, Mouse object. without CPU, Keyboard, Mouse, Monitor object we can’t create the computer object. It means Monitor, CPU, Keyboard, Mouse objects are Dependencies for the Computer object.

Dependency Injection:

Now point comes Dependency Injection so According to Wikipedia, In Software Engineering, dependency injection is a technique whereby one object (or static method) supplies the dependencies of another object.

Completely, Providing dependencies to the dependent object with a technique is called Dependency Injection. For Example:

In the above snippet, I’ve used @Inject annotation from Dagger1. Now we don’t require to pass Monitor, CPU, Keyboard, and Mouse object to computer object. All the dependencies are provided by Dagger itself.

Using Dagger1 :

In dagger, we can declare dependencies using  @Inject annotation. You can add dagger1 dependency using for maven & for Gradle.

 

Dependency Declaration:

We can declare dependencies via two ways. One via Injecting Members and other via Injecting Constructor. Dagger does not support Method Injection.

SATISFYING DEPENDENCIES:

By default, Dagger satisfies each dependency by constructing an instance of the requested type as described above. When you request Computer Object, It will obtain by calling new Computer()  and setting its injectable fields. Now in Dagger, we need to create a Module class with all dependency providers as mentioned below.

Now, all the dependencies being provided through this Module Only. To provide Dependencies we use @Provides, when objects are a different type. When objects are the same type then we use @Named annotation to distinct the dependencies like in the below-mentioned example.

@Named annotation is like scope which is by default in Dagger. It used to distinguish between the same kind of dependencies. Custom scopes are also used for same purpose.

Here @Ram, @HardDisk, @Processor are custom scopes to distinguish all the String dependencies.

BUILDING THE GRAPH:

The @Inject and @Provides-annotated classes form a graph of objects, linked by their dependencies. Obtain this graph by calling ObjectGraph.create() which accepts one or more modules:

This usually requires injecting the main class of a command line app, or the activity classes of an Android app. In our Computer example, the Computer class is used to start dependency injection. We ask the graph to provide an injected instance of the class:

Other Type of Dagger Injection:

Singletons:

Annotate an @Provides method or injectable class with @Singleton. The graph will use a single instance of the value for all of its clients.

The @Singleton annotation on an injectable class also serves as documentation.

QUALIFIERS:

We add a qualifier annotation. This is any annotation that itself has a @Qualifier annotation. Here’s the declaration of @Named, a qualifier annotation included in javax.inject:

STATIC INJECTION:

Warning: This feature should be used sparingly because static dependencies are difficult to test and reuse.

Dagger can inject static fields. Classes that declare static fields with @Inject annotations must be listed as staticInjections in a module annotation.

Use ObjectGraph.injectStatics() to populate these static fields with their injected values:

I hope this tutorial will help you to understand Dependency and dependency injection using Dagger1. Dagger2 is completely different. Please note this point when using Dagger.

For More details about Dagger1, You can refer to link.

 

I’m Dipendra, software developer, and designer who has written code in many of the language known to mankind.Passionate about Technology. In my free time, I love to write articles and build the code.

Leave a reply:

Your email address will not be published.

Site Footer