![Page 2: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/2.jpg)
![Page 3: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/3.jpg)
{concept}Inversion of Control (IoC)
{concept}Dependency Injection (DI)
Googe Guice
RoboGuice DaggerAndroid
Annotations
PicoContainer PlexusSpring core
![Page 4: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/4.jpg)
Dependency Injection (DI)
Makes code ...● more concise ● more modular● easier to test
![Page 5: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/5.jpg)
@ContentView(R.layout.main)public class GuicyActivity extends RoboActivity {
public class GuicelessActivity extends Activity {…@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
![Page 6: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/6.jpg)
@InjectView(R.id.text_output) TextView outputText;
...TextView outputText;...outputText =(TextView)findViewById(R.id.text_output);
![Page 7: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/7.jpg)
@InjectView(R.id.text_output) TextView outputText;
String appName;...appName = getString(R.string.app_name);
![Page 8: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/8.jpg)
@Inject SensorManager sensorManager;
SensorManager sensorManager;...sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
![Page 9: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/9.jpg)
SomeController someController;..someController = new SomeController(this);
public class SomeController { Activity hostActivity;
@Inject public SomeController(Activity hostActivity) { this.hostActivity = hostActivity; }}
@Inject SomeController someController;
![Page 10: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/10.jpg)
@ContentView(R.layout.main)public class GuicyActivity extends RoboActivity { @InjectView(R.id.text_output) TextView outputText; @InjectResource(R.string.app_name) String appName; @Inject SensorManager sensorManager; @Inject SomeController someController;}
public class GuicelessActivity extends Activity { TextView outputText; String appName; SensorManager sensorManager; SomeController someController;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); outputText = (TextView)findViewById(R.id.text_output); appName = getString(R.string.app_name); sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); someController = new SomeController(this); }}
![Page 11: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/11.jpg)
15 LOC → 7 LOC
= more concisene
![Page 12: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/12.jpg)
Dependency Injection (DI)
Makes code ...● more concise ● more modular● easier to test
![Page 13: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/13.jpg)
Bindings
public class MyModule extends AbstractModule {
@Override protected void configure() { // custom binding go here }}
![Page 14: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/14.jpg)
bind(ISomeService.class).to(SomeServiceDummy.class);
![Page 15: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/15.jpg)
bind(ISomeService.class).toInstance(new SomeServiceReal("yes", 1));
![Page 16: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/16.jpg)
@Inject@Providespublic AnotherService provideAnotherService(
ISomeService someService) { return new AnotherService(someService, "bla");}
![Page 17: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/17.jpg)
Bindings allows for
● wiring up components● configuring components
![Page 18: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/18.jpg)
Adding Google Analytics to an Activity● onCreate → startNewSession● onResume → trackPageView● onDestroy → stopSession● Attributes● Support code
![Page 19: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/19.jpg)
Component Bloat-Up● anti pattern: God Class● low coherence● tight coupling● low separation of concernes
![Page 20: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/20.jpg)
public void onCreate(@Observes OnCreateEvent onCreateEvent)
![Page 21: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/21.jpg)
@Inject protected AnalyticsMixin analyticsMixin;
![Page 22: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/22.jpg)
We get:● loose coupling● high coherence● centralized wiring● centralized configuration
![Page 23: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/23.jpg)
Testability (see wikipedia)● controllability: The degree to which it is possible to control the state of the
component under test (CUT) as required for testing.
● observability: The degree to which it is possible to observe (intermediate and final) test results.
● isolateability: The degree to which the CUT can be tested in isolation.
● separation of concerns: The degree to which the CUT has a single, well defined responsibility.
● understandability: The degree to which the CUT is documented or self-explaining.
● automatability: The degree to which it is possible to automate testing of the CUT.
● heterogeneity: The degree to which the use of diverse technologies requires to use diverse test methods and tools in parallel.
![Page 24: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/24.jpg)
@RunWith(RobolectricTestRunner.class)public class SomeServiceTest { @Mock private SomeDAO mockDao; @Mock private Logger mockLogger;
private SomeService cut;
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); cut.someDao = mockDao; cut.log = mockLogger; }
@Test public void testExecute_willPassWhenNotInBeta() { when(mockDao.countSomeStuff()).thenReturn(23); assertTrue(cut.hasEnoughStuff()); verify(mockLogger, times(1)).info("have 23"); }}
![Page 25: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/25.jpg)
Testability (see wikipedia)● controllability ++● observability ++● isolateability ++● separation of concerns ++● understandability +● automatability +● heterogeneity o
![Page 26: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/26.jpg)
Dependency Injection (DI)
Makes code ...● more concise ● more modular● easier to test
![Page 27: Thomas braun dependency-injection_with_robo_guice-presentation-final](https://reader033.vdocuments.us/reader033/viewer/2022060108/554f5f11b4c905c8088b48a6/html5/thumbnails/27.jpg)
References:● DI: http://martinfowler.com/articles/injection.html● Guice: http://code.google.com/p/google-guice/● RoboGuice: https://github.com/roboguice/roboguice● Testability:
http://en.wikipedia.org/wiki/Software_testability● Mockito: http://code.google.com/p/mockito/● Rebolectric: http://pivotal.github.io/robolectric/
Contact:● [email protected]● @tom___b● http://play.google.com → GrooveGrid