Dagger (in a simplified way) is for exactly one thing.
If you have a class that takes a constructor parameter, it provides that parameter.
Now you may wonder why we'd a library for that. It's because you'd want to handle:
- The object you need in the parameter, also needs objects in its construct that also need objects and so on. Which means you'd sit building up this object everytime you needed it. If you change what parameters it has, you now have some awful refactoring to do.
- Maybe you want to RealDb() as a parameter one place and FakeDb() as a parameter in your Android Tests.
- You may want to reuse some of these objects and don't want to carefully manage when each one gets sent where.
If you want to get the full benefits that are involved with this, you could use an interface for the db.
As you go deeper into how it's used, you'll really appreciate something that creates your constructor parameters for you.
Another term for these constructor parameters, since the class that takes them, depends on them, is dependencies.
Dagger therefore provides your "dependencies" and when dependencies are just put into whatever class needs them, we call it "dependency injection".
A mouthful of a phrase which just means "gives you objects where you want them".
The first thing you'll need for that is to look at the concept of Inversion of Control which is a fancy way of saying that don't call "new" in your class, have your objects passed in from outside.
Take a look at that here
In the next article, we'll be looking into Setting up Dagger-Android and why it's easier than you think, and tremendously useful.
Comment if you have any questions! I'll re-write, clarify and simplify based on your feedback.