Science & Technology

Tutorial dagger | raywenderlich.com

Learn about Dependency Injection with Dagger!

Dependency injection is an important technique for building software systems that can be maintained and tested. Perhaps you have already done a dependency injection without realizing it. Dependency injection is not as complicated as its name suggests. This book will guide you through everything you need to know!

This book describes Dagger, Google’s framework for Java, Kotlin, and Android. Dagger helps solve many of the development and performance issues that have plagued reflection-based solutions.

This book is intended for intermediate Kotlin or Android developers who want to know how to implement dependency injection patterns using the Dagger and Hilt libraries.

Before you start

This section describes some things you need to know before you start, such as what you need for your hardware and software, the location of the project files in this book, and so on.

Section I: DI Basics

In this section, you’ll understand why you need to use a Dependency Injection (DI) library like Dagger by learning all about the issues that need to be resolved.

Understand what a dependency is and why you need to control it to create a successful app. You will know the Busso app you are working on and improve through this book. Then take the next step towards implementing a better app that is easier to test and modify.

In this chapter, you’ll learn what dependencies are and how they affect your code. You will also learn how to use abstractions to write better code.


This chapter describes the Busso app that we will work on and improve throughout this book. This is a client-server app where the server is implemented using Ktor.


This chapter takes you to the next step towards implementing a better app that is easier to test and modify. Start by refactoring your Busso app in a world without daggers and hilts.


In this chapter you will learn how scopes and dependencies relate to each other. We’ll start by refactoring how SplashActivity uses the navigator. Then you can define multiple ServiceLocator implementations to help you understand how they depend on each other.


In this chapter, we’ll use the architectural pattern (Model View Presenter) along with what we learned in the previous chapter to create a fully testable app. Use techniques that work in a frameworkless world like Dagger and Hilt.


Section II: Introducing the dagger

In this section, you’ll learn what Dagger is, how it works, and how to reduce the amount of code you need to write manually when implementing dependency injection in your app.

How to use Dagger to handle the injection of constructors, fields, and methods, how to use @Binds to simplify the implementation of @Module if there is an abstraction and its implementation, and @Singleton Learn how to solve very common problems. problem.

In this chapter, you’ll learn what Dagger is, how it works, and how to reduce the amount of code you need to write manually when implementing dependency injection in your app.


This chapter goes into more detail about dependency injection with Dagger. Learn how to use Dagger to handle the injection of constructors, fields, and methods.


In this chapter, you’ll learn how to use different Dagger @Modules in the same app, how to use Dagger’s Lazy interface to optimize launch performance, and how to use the Provider interface to avoid dependency cycling. I will.


In this chapter you will learn more about modules. You can see the benefits of using the @Binds annotation with @Module, how to serve existing objects such as Android contexts, and where optional bindings can help.


Section III: Component and Scope Management

In this section, you will migrate your Busso app from a homemade framework to Dagger. This process involves migrating existing ServiceLocators and Injectors to the equivalent Dagger @Modules and @Components, using @Component.Builder to provide a Builder customized for @Component to existing objects, and @ Learn how to use. Component.Factory as a valid alternative to @ Component.Builder.

The first migration is not optimal — there are still some basic aspects to improve.

In this chapter, we’ll return to working with the Busso app. How to migrate existing ServiceLocators and Injectors to equivalent Dagger modules and @Components, how to use @Component.Builder to provide a Builder customized for @Component to existing objects, and as @Component.Factory Learn how to use it. A valid alternative to @ Component.Builder.


In this chapter, you will also learn about component definitions and how components relate to containers. You can see what the life cycle is, why it is important, and what it has to do with scope. You’ll also learn what @ Singletons, @ Scope are, and how they improve your app’s performance.


In this chapter, you will learn more about @ Components and dependencies. In particular, you’ll learn why @ Singleton isn’t much different from other @Scopes, and why you need a different approach to managing component dependencies.


Section IV: Advanced Dagger

This section details Dagger’s advanced features such as multi-binding. Multi-binding is a very interesting feature of Dagger because it simplifies the integration of new features using the plugin patterns learned in this section.

Implement a simple framework that allows you to integrate new services into your Busso app in a very simple and declarative way. Learn everything you need to know about multi-binding with Set and Map.

In this chapter you will learn what multi-binding is and how to use multi-binding in Set.


In this chapter you will learn how to use multi-binding in Maps. Learn how to use Maps to configure multi-bindings and how to use basic type keys with @ StringKey, @ ClassKey, @ IntKey, and @LongKey. You can also create a simple custom key and use @KeyMap to create a complex custom key.


In this chapter, you’ll learn how to configure and use Dagger in a multi-module app.


Section V: Introducing Hilt

In the last section, you’ll learn everything you need to know about Hilt. Hilt is a dependency injection library for Android that reduces boilerplate when doing manual dependency injection in your project.

Hilt is built on top of the DI library Dagger and benefits from Dagger’s compile-time accuracy, run-time performance, scalability, and Android Studio support.

In this chapter, you’ll learn what Dagger Android is, how it works, and how to reduce the amount of code you need to manually write when implementing dependency injection in your app.


In this chapter you will learn what the Hilt library is and how it reduces boilerplate when doing manual dependency injection in your project.


In this chapter, you’ll learn about the extensions Hilt provides to serve classes from other Jetpack libraries.


In this chapter, you’ll learn how to use Hilt to make it easier to test your code.


This chapter describes how to use Koin to implement dependency injection on your server.


In this chapter you will learn all about assisted injection with Dagger and Hilt.


Tutorial dagger | raywenderlich.com

https://www.raywenderlich.com/books/dagger-by-tutorials/v1.0 Tutorial dagger | raywenderlich.com

Back to top button