We are developing new Android application.
We are trying to use MVVM and Dagger 2. What are all the benefits of using Dagger 2 if we use MVVM?
MVVM and dagger 2 are different things. Dagger 2 implements dependency injection pattern, which is really beneficial for testing, because you can test dependent classes without instantiating classes that require those dependent classes. Also makes code more readable and easy to modify later.
Related
What is best module in Android for dependency handling?
Dragger
Hilt
I'm little bit confused
Hilt is built on top of the Dagger dependency injection library, providing a standard way to incorporate Dagger into an Android application.
Because the Android operating system instantiates many of its own framework classes, using Dagger in an Android app requires you to write a substantial amount of boilerplate. Hilt reduces the boilerplate code that is involved in using Dagger in an Android application. Hilt automatically generates and provides the following:
Components for integrating Android framework classes with Dagger that you would otherwise need to create by hand.
Scope annotations to use with the components that Hilt generates automatically.
Predefined bindings to represent Android classes such as Application or Activity.
Predefined qualifiers to represent #ApplicationContext and #ActivityContext.
Dagger and Hilt code can coexist in the same codebase. However, in most cases it is best to use Hilt to manage all of your usage of Dagger on Android.
I saw this question, but it was from the point of testability.
In my case, I see Dagger as an additional complexity for code (create #Provides methods, inject methods, remember all the right annotations). Should I still use Dagger if I just want to inject singletons like SharedPrefUtils? Is it really faster for this case?
Dagger was not made for creating Singletons and if creating a singleton is the reason you want to add Dagger to your project, then its an overkill. Dagger is a dependency injection framework that helps make the process of providing and injecting dependencies easier in a complex project. The #Singleton would help you ensure that only a single instance of a class is created when satisfying your dependencies it is not an alternative to the Kotlin object, they could even be used together. So like other answers and comments suggest if you have a small project and you could use simple design patterns like Singleton, Factory, and Abstract factory pattern.
Dagger is a dependency injection framework. For very simple applications, you can go with manual injection which will save you time because dagger can be difficult to set up and work with from the beginning. However, if you are planning on a production application which has a very long life span and will contain multiple UIs and multiple classes, then you are definitely going to need a dependency injection framework or strategy along the line.
There is an alternative for Kotlin which is Koin. Koin works more like a Service Locator and is easier to set up than Dagger but I will prefer dagger because it has survived the test of time.
It is up to you whether you want to use automatic dependency injection or manual injection so in the end you weigh your options and see what best fits your use case.
I am new in the MVVM architecture, and every documentations I found on internet use Dagger with the MVVM architecture. Dagger is style fuzzy for me and seems to me to be over-complicated to just create an android app. So my questions are simple:
Why should/must I use dagger with MVVM architecture? And why Dagger comes not as often with MVP architecture than with MVVM architecture?
Is it possible to develop an android app with MVVM architecture and without dagger?
I disagree with people who said that dagger is just library.
Dependency Injection : Is a concept and a way to code, and dagger make it more easy to take advantage of dependency injection with annotations.
Also the Sun-Flower project from google didn't use dagger while using MVVM pattern.
You don't need Dagger in the MVVM architecture or the MVP architecture - Dagger is just a dependency injector library. You can use lazy loading in Kotlin now or Koin as a replacement for Dagger. There is no dependency on Dagger for the architecture patterns.
Dependency injection is just an idea.
Dagger2 is a library which uses annotation processing to help with dependency injection boilerplate.
Even Jake Wharton says Dagger2 can be overkill for smaller apps.
It's absolutely possible to develop an MVVM app without a DI framework. Technically you don't need DI at all, but it does tend to make things easier - especially in regards to testing.
I'm reading about Dependency Injections and found 2 libs that get my attention, AndroidAnnotations and Dagger 2. What I saw is that AA has a lot of functionalities including DI, but most of the developers are using Dagger 2 for DI.
I was wondering what is the diference between DI with AA and DI with Dagger 2? If I use AA it means I don't need Dagger 2?
I couldn't find much information for DI with AA and comparison with other libraries.
Any info would help a lot.
I do not think AA and Dagger can be compared.
Dagger is a general dependency injection library, with lots of capabilities. It is designed to run on Android as well, but it does not need Android, it can be applied on pure Java projects. It has lots of dependency injection features for a fully code-generation based dependency injector.
AndroidAnnotations is an annotation-based framework for Android. It does have a limited dependency injection module (which is only a small subset of AA), however that is not its main feature. It adds annotation based, boilerplate removing APIs for lots of thing for Android, which are used in every project and normally require an awful lot of unnecessary code, like view and resource injection, event handling, instance state restoration, threading, etc. You can see all the use cases of AA here.
Dagger and AA can coexist, actually it really makes sense to use the sophisticated dep injection from Dagger and the lot of features of AA together (i do in all of my projects).
Disclaimer: i am an AndroidAnnotations developer.
recently I have created one sample application by implementing Dagger 2 and Android Architectural Components (Room and Viewmodel) which can help you understand dependency injection using dagger library along with MVVM architecture.
Here is the github project link
There is one approach mentioned in http://www.vogella.com/tutorials/Dagger/article.html#connecting-consumers-and-providers and other approch without using Component here http://antonioleiva.com/dagger-android-part-2/.
First, please note that Dagger 2 and Dagger are not the same thing. If you read ObjectGraph you are probably reading about Dagger (1).
You can not use Dagger 2 without components. Components are the objects that hold your dependencies and know how to provide and inject them. I think the same holds true for Dagger 1 about their ObjectGraph. If you don't have the means to provide your dependencies, you can't use DI.