Thomas braun dependency-injection_with_robo_guice-presentation-final

888 views

Published on

Published in: Business, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
888
On SlideShare
0
From Embeds
0
Number of Embeds
131
Actions
Shares
0
Downloads
19
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Thomas braun dependency-injection_with_robo_guice-presentation-final

  1. 1. Dependency Injection with RoboGuice ©2013 Tom Braun <tom.braun@apps4use.com>
  2. 2. {concept}Inversion of Control (IoC) {concept}Dependency Injection (DI)Googe Guice Spring core PicoContainer Plexus AndroidRoboGuice Dagger Annotations
  3. 3. Dependency Injection (DI)Makes code ...● more concise● more modular● easier to test
  4. 4. public class GuicelessActivity extends Activity {…@Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main);@ContentView(R.layout.main)public class GuicyActivity extends RoboActivity {
  5. 5. ...TextView outputText;...outputText =(TextView)findViewById(R.id.text_output);@InjectView(R.id.text_output) TextView outputText;
  6. 6. String appName;...appName = getString(R.string.app_name);@InjectView(R.id.text_output) TextView outputText;
  7. 7. SensorManager sensorManager;...sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);@Inject SensorManager sensorManager;
  8. 8. SomeController someController;..someController = new SomeController(this);@Inject SomeController someController;public class SomeController { Activity hostActivity; @Inject public SomeController(Activity hostActivity) { this.hostActivity = hostActivity; }}
  9. 9. 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); }}@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;}
  10. 10. 15 LOC → 7 LOC= more concisene
  11. 11. Dependency Injection (DI)Makes code ...● more concise● more modular● easier to test
  12. 12. Bindingspublic class MyModule extends AbstractModule { @Override protected void configure() { // custom binding go here }}
  13. 13. bind(ISomeService.class).to(SomeServiceDummy.class);
  14. 14. bind(ISomeService.class) .toInstance(new SomeServiceReal("yes", 1));
  15. 15. @Inject@Providespublic AnotherService provideAnotherService( ISomeService someService) { return new AnotherService(someService, "bla");}
  16. 16. Bindings allows for● wiring up components● configuring components
  17. 17. AddingGoogle Analytics to an Activity● onCreate → startNewSession● onResume → trackPageView● onDestroy → stopSession● Attributes● Support code
  18. 18. Component Bloat-Up● anti pattern: God Class● low coherence● tight coupling● low separation of concernes
  19. 19. public void onCreate(@Observes OnCreateEvent onCreateEvent)
  20. 20. @Inject protected AnalyticsMixin analyticsMixin;
  21. 21. We get:● loose coupling● high coherence● centralized wiring● centralized configuration
  22. 22. 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.
  23. 23. @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"); }}
  24. 24. Testability (see wikipedia)● controllability ++● observability ++● isolateability ++● separation of concerns ++● understandability +● automatability +● heterogeneity o
  25. 25. Dependency Injection (DI)Makes code ...● more concise● more modular● easier to test
  26. 26. 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:● tom.braun@apps4use.com● @tom___b● http://play.google.com → GrooveGrid

×