Reputation: 14571
I've been missing around one year in Android development and boy, what a large chunk to chew: Kotlin, LifeCycle, Room, ViewModel, Dagger and so on.
I am trying to get my head around the new MVVM recommended pattern and while it's really great to see something more "standardized" there are some things unclear.
To have things in a simple way, let's have a brief sample.
First, create the Entity
@Entity(tableName = "user_table")
public class User {
@PrimaryKey
@NonNull
@ColumnInfo(name = "name")
//vars, getters, setters
}
Then create the DAO
@Dao
public interface UserDao {
@Insert
void insert(User user);
...
}
Then, I need to make the Repository. This is basically where my questions start.
I see many examples, on official docs also using Dagger2. While I still struggle to fight with Dagger to proper understand dependency injection my question is, why do I need to use Dagger? I do understand that decoupling components allows easier testing, but is this necessarily for small projects? Why not interact between ViewModel, Repository, DAO directly without injection (maybe having classes passed on constructors)?
As a side note, probably it's a good point to start with Kotlin. While I find it pretty cool I see that many parts of the official docs/guides still only have Java samples... So I kind of want to learn the new stuff with old Java but in the same time, since my previous projects was on javascript, the switch to kotlin wouldn't be that hard...
Upvotes: 3
Views: 1105
Reputation: 1408
Dependency injection has a lot of benefits. It promotes decoupling and testability as you already pointed out. But what's really attracting from dagger is the ease of dependency management. Dagger figures out your dependency graph at compile time, even provides mechanism to localize (or globalize) lifecycle and scope. It is component based as well so it can be easily architected.
Using dagger in small projects is arguable. If your project contains simple dependency graphs and is not an evolving one, the added code (and footprint) from dagger can be a little too much (not to mention the learning curve if you are not yet familiar).
But it is always a good thing to use dependency injection in my opinion. It promotes SOLID principle and cleaner architecture.
Upvotes: 3