DEV Community

Aniket Kadam
Aniket Kadam

Posted on β€’ Edited on

6 2

Dagger, the easy way Part 1: Why Should You Care?

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:

  1. 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.
  2. Maybe you want to RealDb() as a parameter one place and FakeDb() as a parameter in your Android Tests.
  3. 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".

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.

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (7)

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard β€’

It would be good if you do what Google never cared to do, which is to explain that using dependency intention is the important part.

Should you use Dagger or Koin or manual dependency injection?

the right answer here is: it depends on your context, there is a trade-off involved.

Collapse
 
aniketsmk profile image
Aniket Kadam β€’

Through my and other's experiences I've found that dependency injection makes things simpler when you have an architecture that is well separated.
Which means that you've got interfaces and classes that do one thing each and they need to be created and passed into other classes to enable progressively larger parts of a feature.
That's a bit further ahead but I'll try to think of how to explain it that doesn't involve a whole lot of cognitive load for the reader, thanks!

Collapse
 
jmfayard profile image
Jean-Michel πŸ•΅πŸ»β€β™‚οΈ Fayard β€’

I totally agree on dependency injection.
I just added that the pattern was more important that the particular framework you are using.
And that once you know the pattern, picking up the framework is easy.
but not the other way around: struggling with Dagger annotations doesn't do much to help the reader understand the DI pattern.
also Dagger documentation is crap.

Bonus: if you explain the pattern, with Dagger as a mere illustration for Android devs, your future article will be useful for much more Dev.to readers than just for people doing Dagger on Android.

Collapse
 
funkymuse profile image
FunkyMuse β€’

Dagger for Android is deprecated.
So you shouldn't care.

Collapse
 
mwshubham profile image
Killer β€’

@aniketsmk any comment on this.

Collapse
 
aniketsmk profile image
Aniket Kadam β€’

It's covered in the article, deprecation doesn't mean much if there's nothing to replace it with.
I do talk about a tradeoff of what exists currently that you might want to take that tradeoff with, also in the article.

Thread Thread
 
mwshubham profile image
Killer β€’

True, deprecated not replaced. Good Article. Thanks for sharing. :)

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay