Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MVVM ViewModel and Repositories for each entity?

With the Android Architecture components and the MVVM pattern I have some question.

Based on most examples around the web there are usually simple examples.

  1. Have an entity for Room
   @Entity    public class User{    ...    } 
  1. Have a DAO
    @Dao     public interface UserDao{     ...     } 
  1. Have a repository
   public class UserRepository{     } 
  1. ViewModel
    public class UsersListViewModel extends AndroidViewModel{     ....     } 

Now let's extend this and beside user have user_access and user_actions for instance, so have 3 tables.

Questions:

  1. For each table in Room I create entities. Should I have 3 Dao one for each entity (userDao, userAccessDao, userActionsDao) or just a general AppDao class?

  2. Same goes for Repository. One repository for the entire app or Repositories for each Entitiy (RepositoryUser, RepositoryUserAccess, RepositoryUserActions?

  3. If my app has one main activity and multiple fragments, should I create one ViewModel for each fragment?

like image 995
Alin Avatar asked Jun 13 '18 19:06

Alin


People also ask

Why do we need repository in MVVM?

What is Repository in Android's MVVM architecture? Repository is a class which purpose is to provide a clean API for accessing data. What that means is that the Repository can gather data from different data sources(different REST APIs, cache, local database storage) and it provides this data to the rest of the app.

How many Android repositories are there?

You need one repository for each model (data).

What is the use of repository class in MVVM?

The repository class isolates the data sources from the rest of the app and provides a clean API for data access to the rest of the app. Using a repository class ensures this code is separate from the ViewModel class, and is a recommended best practice for code separation and architecture.

What is Repository pattern in MVVM?

The knowledge of this persistence, i.e., the persistence logic, is encapsulated inside a class called “Repository”. Repository pattern implements separation of concerns by abstracting the data persistence logic in your applications.

What is viewmodel (MVVM)?

Model — View — ViewModel (MVVM) is the industry-recognized software architecture pattern that overcomes all drawbacks of MVP and MVC design patterns. MVVM suggests separating the data presentation logic (Views or UI) from the core business logic part of the application. The separate code layers of MVVM are:

What is an example of MVVM in Android?

Here is an example of a single activity User-Login android application to show the implementation of the MVVM architecture pattern on projects. The application will ask the user to input the Email ID and password. Based on the inputs received the ViewModel notifies the View what to show as a toast message.

What is the relationship between view and ViewModel in Android?

ViewModel does not hold any kind of reference to the View. Many to 1 relationship exist between View and ViewModel. No triggering methods to update the View. There are 2 ways to implement MVVM design pattern in Android projects:

What are some examples of Android architecture components and MVVM pattern?

With the Android Architecture components and the MVVM pattern I have some question. Based on most examples around the web there are usually simple examples. @Entity public class User { ... } @Dao public interface UserDao { ... } public class UsersListViewModel extends AndroidViewModel { .... }


1 Answers

1

You should have contextual DAOs, let's say an UserDao which should contains the queries related to the users, if you have posts in your app, you should have a PostDao for everything related to posts.

2

Same logic for repositories, remember the Single Responsibility Principle for classes, sticking to that principle you should have repositories for each kind of entities separated (UserRepository, PostRepository...).

3

Following all the new concepts described as Jetpack you should have one viewmodel per fragment, unless for one strange reason you have two fragments that need the exact same logic, and that is very unlikely to happen since the objective of a fragment is to be reused.

like image 141
Eury Pérez Beltré Avatar answered Oct 02 '22 15:10

Eury Pérez Beltré