Sunday, April 14, 2013

Dependency injection android (roboguice review)

Pre-req: you should know about development in android.
I am using: For this tutorial I am using windows xp, maven, android 2.33 , spring restfull android, robojuice

Hi guys,


People those are familiar with spring and Inversion of control, They knows that IOC provides good level of abstraction in managing object(s) i.e initializations, object management, association management. And also decrease lines of code. So lets see roboguice.

To install this first create a maven base project.


1) Add dependencies for roboguice into the project (POM.XML).

 <dependency>
     <groupId>org.roboguice</groupId>
     <artifactId>roboguice</artifactId>
     <version>2.0</version>
  </dependency>
            <dependency>
               <groupId>com.google.code.findbugs</groupId>
               <artifactId>jsr305</artifactId>
              <version>1.3.9</version>
           </dependency>
Reference


2) What we have?
    We can megre 2 steps i.e declaration and initialization, we can avoid object creation and management on different  level (code liens) at class level.


We can create service and bind it with our activities/fragments. This provides alot of ease in fast development.

we can also bind android resources string/arrays/color/config etc to objects.

Reference

3) List of warped components 
   Robo juice warped many android components, list of these components are below

  • RoboActivity
  • RoboListActivity
  • RoboExpandableListActivity
  • RoboMapActivity
  • RoboPreferenceActivity
  • RoboAccountAuthenticatorActivity
  • RoboActivityGroup
  • RoboTabActivity
  • RoboFragmentActivity
  • RoboLauncherActivity
  • RoboService
  • RoboIntentService
  • RoboFragment
  • RoboListFragment
  • RoboDialogFragment

To use feature of robojuice, all you need is to extend your components with one of respective above warped components. for example if you want to use robojuice dependency management in your activity then extent your class with  RoboActivity  instead of Activity.

4) Create and activity
  Lets create a activity with robojuce, lets say we have  list of software houses.
 So I have a list of software houses in string.xml


 and my activity is like below:




5) Lets create a factory to execute web service calls (Powered by Spring rest for android)

i) Create an interface  ResponseFctory 




ii) Create implementation of ResponseFctory , 
       Add annotation @Singleton on the top of implementation, because we are creating a factory of singleton scope.




iii) Create configuration file to bind Interface with implementation
   A class extends AbstractModule and bind interface with implementation in configure function
with command

bind(ResponseFctory.class).to(ResponseFactoryImpl.class); 
below is example


iv) Register custom AbstractModule  with robojuice 
Create a robojuice.xml file in res/values folder. Add path of your Configuration class like below

<resources>
    <string-array name="roboguice_modules">
        <item>com.testguice.config.Configuration</item>
    </string-array>
</resources>



v) Use this factory inside your activity like below





6) Benefits

i) Decrease association. We can bind parameters with function. Hence increase decoupling.
ii) Promote polymorphism, Singeltons and interfaces. Hence increase decoupling.
iii) Object creation and management. you dont need to initialize objects on different level (lines in code) of class.. Hence increase readability
vi) Decrease lines of code. Enforce  best practices.

Source code available.

2 comments: