Android, Java, Software Development

Android Dagger2 greenDAO ORM With Service And DAO Layers

This post shows how to create a sample Android app that uses Dagger2 and greenDAO ORM. The codes are also organized in service and DAO layers. Previously, we touched briefly on greenDAO ORM for SQLite. This time we’re using the Dependency Injection framework Dagger2. The framework will help to instantiate our classes and inject the objects accordingly.

Requisites

The requisites are similar to those in Android GreenDAO ORM for SQLite. However, this time we include Dagger2-related dependencies. In the end, we’ll have the following codebase structure.

There are two fragments and one activity. For brevity, our codes only retrieve the lists of Person and Pet entities. The MainActivity class is where we trigger the dependency injection, and it has a reference to our Dagger2 component object. On the other hand, the two fragments use the PersonService and PetService to retrieve entities, nothing else. If everything works, there shouldn’t be any Exceptions from these services.

Update Android build.gradle for Dagger2 and greenDAO ORM Dependencies

Modify the build.gradle file as shown below. Note the highlighted line. We included the line classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0'.

Next, we update the app/build.gradle file, as shown below. We added the four highlighted lines.

Android greenDAO ORM Entities

Then, we create our greenDAO ORM entities. Before building the whole Android project, we have the following original Java codes. For the Person entity, we have these codes.

For the Pet entity, we have the following.

Then, build the project via the Build > Make Project menu item to generate the DAO and other ORM-related classes.

The Service Classes

Next, we can write our services. For the PetService class, we have the following codes.

Lastly, we have the codes for the PersonService class.

Android Dagger2 Component and Modules

Our codes have a Dagger2 component and two modules. The TurretaComponent interface has the following codes.

For simplicity, we have three inject methods explicitly for MainActivity, FirstFragment, and SecondFragment classes. The classes call these methods in their onCreate methods. Meanwhile, there are two modules in this class. The DBModule holds a reference to an application context and has the following codes.

Unlike in the Android greenDAO ORM For SQLite Example, the new codes open or create the database when Dagger2 tries to instantiate a DaoSession class. Note that Dagger2 generates the DaoSession class when we build our project via the Build > Make Project menu option. Therefore, the class may not be readily available while we code.

The other module, the AppModule, uses the DBModule to reference the DaoSession object to create the services.

Try building the whole project to check everything is okay so far before moving on.

Modify Activity and Fragments To Use Dagger2

In this section, we modify the activity and the two fragments as little as possible. The MainActivity class will be the triggering point where the Dagger2 dependency injection to kickstart.

First, we add the following codes. The getter method allows our fragments to get a reference to our Dagger2 component via the getActivity method, as shown later.

Second, modify the onCreate method to include these codes.

Now, we go on to the FirstFragment class. Add the following codes.

Then, inject the Fragment object into the Dagger2 context, and try retrieving a list of Person entities in the onCreateView method.

Next, we do something similar to the SecondFragment class.

Testing Our Android App

When we start the Android app and switch between the fragments by clicking the lone button, we can see codes write logs on the console. Along with the log comes the SQL SELECT queries used to try to retrieve records.

That’s it!

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like