Depedency injection and Kotlin - android

What's the point of using Dagger2 and Kotlin ?
Dagger2 docs says:
"Constructor injection is preferred whenever possible because javac will ensure that no field is referenced before it has been set, which helps avoid NullPointerExceptions."
The main reason why Kotlin exists is to provide null safety.
So is it worth to use Dagger2 with Kotlin ?

This quote (which I don't directly see in the docs, but never mind) says that one way of using Dagger (constructor injection) is more null-safe than another (method injection).
It doesn't say anything about using Dagger to provide null safety compared to not using Dagger.
If you use Dagger's constructor injection with Kotlin, your properties will be normal non-nullable ones; if you use method injection, you'll need to make them nullable or use lateinit. So constructor injection still provides more null-safety than method injection when used with Kotlin.

Related

Can Dagger-2 constructor injection be overridden for testing?

I am using Dagger 2, and I'm trying to understand how to use different configuration for testing.
If I use field injection, I can swap my Modules with fake Modules.
But for classes that have constructor injection, I can't figure out how to do it.
So, is this a reason to avoid constructor annotation? or is there a way to exchange such a class with a fake?

Constructor vs Field vs Method Injection Dagger 2

I have been learning Dagger 2 for a week and in the tutorials I read, most of them wrote that it is preferable to use Constructor injection over Field and Method injection.
I am confused and wanted to make clear, when should I be using Constructor injection, and when Field and Method injection.
Thanks in advance.
I can't say this is the definite answer, but I'd like to share my opinion. I prefer constructor injection because you're forced to create the object by fulfilling its dependencies. In other words you'll never end up in the situation that you call an object's method and because some dependency wasn't set, you get a null pointer exception (unless you set it as null on purpose in which case you're asking for it). I tend to always use constructor dependency injection everywhere.
That said, sometimes it's simply not possible. In Android for example the Activities are created by the system and hence we don't usually have our own constructor to call and pass in the dependencies. Here I often use field dependency injection. I do this because there's a lot of tools out there that help you out with this.
A third options is through setters. Personally I never used it. Not entirely sure where this would be needed. Perhaps when the dependency has to change at run time? Not sure.

With Dagger 2, is it possible to inject on a same class from different subcomponents or from different components?

It seems to be impossible but I can't find it written explicitly. Is there a clear reason for that ?
Thanks
That's not possible. A component can either inject everything at once or the compilation will fail with a cannot be provided error, listing what it's missing.
After all you can't partially call a constructor (if using constructor injection) and also partially injecting fields would be rather indeterministic about which objects were injected when, how, or with which scope. If both components could supply a dependency, which should provide it? Do you create and inject the object twice? What if another object depends on it in the other component? It would create more confusion than any good it could do and provide a source for a lot of errors and unexpected behavior.
Only one component can be used to inject inside a given class. You can have several subcomponents dependencies installed on it though.

Dagger 2: Providing dependencies in Application Module vs injecting them

I was trying to add Dagger 2 to my android app.
As far as I understand, Dagger will construct my object(which I am trying to inject) as long as its' dependencies are provided(in a Module) or they are injected using some form of injection(constructor/method).
I would like to know if there's a distinction between when a dependency should be provided in a Module(say Application Module) vs when its' injected using a constructor injection, and if there is any rule of when I should do which?
Both are the same. Constructor injection basically eliminates the need to write a provider method. As a rule of thumb, I mostly use it for classes with a no-args constructor for easy injection, like Util classes.
There's no difference really. As long as Dagger knows how to construct an instance, that's all that matters.
The reason there are two ways to do it is that you don't always have the ability to use constructor injection, for instance if the class is part of a library that you are using but which you don't have the source (and so you can't add #Inject on one of the constructors).

AndroidAnnotations and constructor injection

I'm going through androidannotations cookbook and I can't find way to inject dependencies to Bean via constructor.
Is it possible at all with this library? I have everything else up and running, but I just can't fix this single problem :/
AndroidAnnotations does not provide this feature currently. Setter injection maybe added in the next release.
Use field injection, if possible.

Categories

Resources