Your SlideShare is downloading. ×
Thomas braun dependency-injection_with_robo_guice-presentation-final
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Thomas braun dependency-injection_with_robo_guice-presentation-final

517

Published on

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

No Downloads
Views
Total Views
517
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
15
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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

×