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 UserRepository{ }
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:
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?
Same goes for Repository. One repository for the entire app or Repositories for each Entitiy (RepositoryUser, RepositoryUserAccess, RepositoryUserActions?
If my app has one main activity and multiple fragments, should I create one ViewModel for each fragment?
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.
You need one repository for each model (data).
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.
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.
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:
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.
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:
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 { .... }
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.
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...).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With