jeudi 28 novembre 2019

Why ViewModel's object shouldn't manipulate database directly?

I am learning Android Architecture Components.

For exemple, and be more easier to understand, If i want to build a TO DO LIST app, my item creation DAO should be

@Dao
public interface ItemDao {
    @Insert
    long insertItem(Item item);

and my viewModel could be use this DAO to insert an item in my TODO list. But, in architecture component, it is recommanded to NOT manipulate the database by the viewmodel but by the repository.

So, the code should be like that

public class ItemDataRepository {

    private final ItemDao itemDao;

    public ItemDataRepository(ItemDao itemDao) { this.itemDao = itemDao; }

    // --- CREATE ---

    public void createItem(Item item){ itemDao.insertItem(item); }

It seems redundant when we cannot understand why.

My question is : why?

Aucun commentaire:

Enregistrer un commentaire