8

Dagger2 is hard, but it can be easy #2

 3 years ago
source link: https://www.funkymuse.dev/2021/03/dagger2-is-hard-but-it-can-be-easy-2.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Dagger2 is hard, but it can be easy #2

We're here again telling ourselves that Dagger2 is easy, as we've seen in the first part.

To demystify some myths about Dagger2 we'll see it's simplicity in the smallest blog post so far.

From the previous article we had a @Module, @Component and @Inject.

What if I told you that you can achieve the simplest dependency injection with @Inject and @Component, without the need of a module...

As I previously mentioned, Dagger2 is really easy, Android makes it hard, enough talk, showing you the code.

and in our MainActivity

run it and it works.

Our component looks simple enough

@Component
interface OurFirstComponent {
fun giveGraphModulesToMainActivity(mainActivity: MainActivity)
}

 and we just commented our Module and removed the (modules = [OurFirstModule::class]) annotation's parameter.

See?
Simple.

What does @Inject do?

It gives you the object you annotated @Inject with, in our case the Logger we created.

Annotating an object you want to get with @Inject to get that same object later on using @Inject but as a variable.

Since we mentioned variables, you might ask yourself why the variables can't be private?

Dagger does not support injection into private fields...

Oh okay Dagger, but why?

When the graph has your object it knows about the package and it's name when you annotated it with @Inject since this is transformed by an annotation processor, that's a limitation of the annotation processor, it needs to be visible in the package, so that means you can make the variable internal in this case and it will work, basically Dagger sets the lateininit value to the one it has in the graph but it can't be private due to the the variable not visible out of the class.

Part #3

19:50FunkyMuseAndroid, Dagger, KotlinNo comments

Related Posts:

  • unnamed.jpgOptimize the build speed for your Android project

    We've all came to a decision to go and do something else while waiting for the build.gradle to finish, something like: people get married or divorced, learn to fly a plane, build a rocket ship out of LEGO or tak… Read More

  • no-img.pngDagger 2 is hard, but it can be easy #3 In the previous post we've seen simpler form of injection with Dagger, but we haven't talked about the types of injection that Dagger2 has to offer.So far we've seen variable injection but there are construction injection an… Read More
  • no-img.pngView binding for the lazy If you ever looked at this image and wondered, why this blog post is written when I as a developer was promised a low amount of code needed for view binding to work, well technically that's true, but also false at the same t… Read More
  • a413105fcf3666eca7dcef314b5c2dd0.gifDagger2 is hard, but it can be easy #2 We're here again telling ourselves that Dagger2 is easy, as we've seen in the first part.To demystify some myths about Dagger2 we'll see it's simplicity in the smallest blog post so far.From the previous article we had a @Mo… Read More
  • 7fd1edc4fe92b9ced76e1f30ce90121c.gifDagger2 is hard, but it can be easy #1 One day you decided to use Dagger2 and then you were lost, don't worry, most of us were already there, Dagger2 is easy, you'll see, it's literally one interface, let's start.A little bit of history, Dagger2 is a continuation… Read More

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK