Why is MVVM always presented with Dagger? - android

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.

Related

I've facing problem in android dependecy injection

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.

Is there any way to use MVVM software architectural pattern in a KMM project without using moko mvvm?

I'm new to KMM (Kotlin Multiplatform Mobile). I need to use MVVM in this project and I just need to use jetpack for ViewModels. So, how can I organize MVVM architecture in the KMM project?
You can create an expect/actual declaration for your viewmodels, where you can use the architectural ViewModels in your Android actual declarations.
This example does the exact same thing.
The expect/actual documentation you can find here

Kotlin Dependency Injection Framework Choose

I have a question. I have an application that I wrote by extracting data from Api. And I want to add dependency injection to it. Which library will work best and why? How should I choose?
In Android there are multiple libraries to support dependency injection here are few of most used ones by developers.
The ones provided by google are Dagger and Hilt. But they have a bigger learning curve compared to other DI libraries. Still I would recommend to you these in your project.
Hilt is the latest counterpart and is a written over the existing Dagger DI and comes with all the abilities of Dagger hence it is recommended from now on over dagger since it provides a standard way to use DI in your application which was missing in Dagger.
Check out about Dagger from this link
Check out about Hilt from this link
Both the above DI libraries automatically generates code that mimics the code you would have written manually to provide dependency. The code is generated at compile time and therefore if there comes any issue with providing dependency it will be shown at compile time hence avoiding runtime issues regarding fulfilling dependencies.
There are Kotlin specific dependency injection libraries too such as Koin which is more easy to learn and implement compared to Dagger.
Check out about Koin from this link
Koin is usually considered for small to medium sized projects while Dagger and Hilt is considered for medium to large sized projects.
Dagger hilt library is best for dependency injection very less code for injection you can simply inject anything also no need to provide any view model factories for your view models

MVVM and Dagger 2 - Benefits

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.

AndroidAnnotations and Dagger 2

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

Categories

Resources