Skip to content

Lesson 5 Local Data Source: Room Database

FranGarc edited this page Jul 22, 2023 · 1 revision

Description

Persist the elements of the List using a local Room Database.

Requirements

  • Create a Room Entity (Data Transfer Object) to represent the List Item (a numeric id, a String for the element value)
  • The database needs two query-methods:
    • Insert method that takes a parameter of the entity type
    • Get All method, no parameters and returns a Flow with a list of entity type elements.
  • Create a domain entity for the list element (numeric id and String for element value). Include an extension function to transform a data entity element into a domain entity element.
  • Create an UseCase for each of the query-methods. Each will call the data methods using the Repository Pattern.
  • Refactor the ViewModel so it calls upon the necessary UseCase.
  • Edit and Delete button should display an error message "functionality not available"

What to research

  • Room:
    • Configuration
    • Database
    • Entity
    • DAO
    • Observable query vs asynchronous query
  • Repository pattern.
  • UseCases.

Tips & Advice

  • The Room Entity classes represent the data layer Models.
  • The domain layer has "entity" classes that are very simple data classes that contain just the information the view layer needs. In the data layer they usually are called Models or DTOs, in the domain layer they're called "Domain Entities" or "Domain Models".
  • Your ViewModel should get the flow from the database just once, but in a way that any changes to the database contents (i.e. inserting new elements) get reflected inmediately.
  • This is a local database, so the database code should be in a local subpackage of the data layer.

Clone this wiki locally