‫Hello, my friends, and welcome back.
‫In this video, we'll learn about repository.
‫We succeeded in creating our room database.
‫The next step is to create the repository.
‫A repository in Mvvm architecture is an intermediary layer between the ViewModel and the data sources,
‫such as a database or network service.
‫It abstracts the details of data retrieval and provides a clean API for the ViewModel to interact with.
‫It's mainly used to manage multiple resources of data.
‫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.
‫This is the function of the repository and the main purpose of creating the repository to gather data
‫from different data sources, rest APIs, cache local database storage and providing this data to the
‫rest of the app.
‫When you have only one source of your data, such as our application, the room database, you don't
‫need to create repository, but for demonstrating the usage and the main role of repository, I'll use
‫it in our application.
‫I'll create a new Java class and I'll name it as my repository or repository.
‫We search for the available data sources and in our application we have only the room database, the
‫offline database.
‫So for that I'll create private final contact Dao and I'll use and I name it as contact Dao object.
‫So when dealing with room databases, you need to deal with the contact dao in the repository.
‫Alt+ enter to create the constructor.
‫Having this contact dao as a parameter and initializing.
‫Okay, so this is our repository.
‫Now.
‫This repository encapsulates data access and retrieval logic.
‫It receives a contact Dao object in its constructor, which provides access to the local database,
‫assuming you are using room.
‫And this is what we've done and every method created in the Dao, you should mention it here in the
‫repository.
‫So I have three methods insert, delete and get all contacts I need to to use these methods from the
‫repository.
‫For that.
‫I'll start with the first method.
‫Add contact.
‫It receives a contact object and I need to call this method insert in the Dao.
‫So I'll use this contact dao object dot insert.
‫And here I need to pass this parameter as a parameter inside this insert method.
‫It's just managing all the methods in the different data sources.
‫Again, if you have only one data source, you don't need to use the repository.
‫Publicvoid delete contact receives a contact object and execute the contact Dao dot delete method and
‫passing the contact as the argument for this delete method.
‫The last step is the get all contacts and it's returning type is a list of contacts.
‫So for that I need to create here public list of contacts object.
‫Get all contacts and I need to call the contact Dao dot get all contacts Alt+ enter to import the class
‫of the list and here I need to use the return statement.
‫So these are three methods are listed in the repository that are similar to the Dao.
‫If you have any source of data, you need to mention all the methods here in the repository.