GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing frogermcs.github.io — Dependency injection with Dagger 2 - the API – froger_mcs dev blog – Coding with love {❤️} Dependency injection with Dagger 2 - the APIThis post is a part of series of posts showing Dependency Injection in Android with Dagger 2. Today I’d like to delve into a fundamentals of Dagger 2 and go through a whole API of this Dependency Injection framework. Dagger 2In my previous post I mentioned what DI frameworks give us - they wire everything together with as small amount of code as it’s possible. Dagger 2 is an example of DI frameworks which generates a lot of boilerplate for us. But why is it better then the others? Rigth now it’s the only one DI framework which generates fully traceable source code which mimics the code that user may write by hand. It means that there is no magic in dependencies graph construction. Dagger 2 is less dynamic than the others (no reflection usage at all) but simplicity and performance of generated code is on the same level as the hand-written code. Dagger 2 fundamentalsHere is the API of Dagger 2: Also there are other elements defined by JSR-330 (stardard for Dependency Injection in Java) which are used in Dagger 2: Let’s go through all of them. @Inject annotationFirst and the most important for DI is Constructor injection
All parameters are taken from dependencies graph. Limitation in this case is that we cannot annotate more than one constructor in class with Fields injectionThe other option is to annotate specific fields with But in this case injection process has to be called “by hand”, somewhere in our class: Before this call our dependencies are null values. Limitation in fields injection is that they cannot be Methods injectionThe last way to provide depencencies with All method parameters are provided from dependencies graph. But why would we need method injection? It will work in situations when we want to pass class instance itself ( @Module annotation
@Provides annotationThis annotation is used in @Component annotationThis annotation is used to build interface which wires everything together. In this place we define from which modules (or other Components) we’re taking dependencies. Also here is the place to define which graph dependencies should be visible publicly (can be injected) and where our component can inject objects. Example source code for Also @Scope annotationAnother part of JSR-330 standard. In Dagger 2 Now a couple less important, not always used things: @MapKeyThis annotation is used to define collections of dependencies (Maps and Sets for now). Example code should be selfexplaining: DefinitionProviding dependenciesUsage
@Qualifier
Naming dependenciesInjecting dependenciesAnd that’s all. We’ve just got to know all important elements of Dagger 2 API. App exampleNow it’s time to check our knowledge in practice. We’ll implement simple Github client app which is built on top of Dagger 2. The ideaOur Github client has three activities and very simple use case. Its whole flow:
Here is how our app will look like:
Under the hood, from DI perspective our app architecture looks like below:
In short - each activity has its own dependencies graph. Each graph (
Speaking about Examples from documentation: Provision methodsMoreover we also have to define where we want to inject dependencies (via member-injection). In our case Examples from documentation: Members-injection methodsImplementationI won’t delve to much in source code. Instead just clone GithubClient code and import it in the newest Android Studio. Here is a couple hints where to start: Dagger 2 installationJust check AppComponentStart exploring GithubClient project from
Scoped componentsTo check how Activies Components are created just start exploring from Also in this place we’re providing AppComponent instance (because The rest is up to you. Seriously - try to figure out how everything fits together. And in next post we’ll try to take a look closely on Dagger 2 elements (how they work onder the hood). Source codeFull source code of described project is available on Github repository. AuthorMiroslaw Stanek If you liked this post, you can share it with your followers or follow me on Twitter!
Written on June 10, 2015
|
| You are visitor 01402809 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |