Pro Android with Kotlin


Download 5.42 Mb.
Pdf ko'rish
bet103/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   99   100   101   102   103   104   105   106   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

116
CHAPTER 8: APIs
Configuring Your Environment for Room
Since Room is a support architecture component, you must configure it in your Android 
Studio build script. To do so, open the module’s build.gradle file (not the one from the 
project!) and on top level (not inside any of the curly braces) write the following:
apply plugin: 'kotlin-kapt'
This is the Kotlin compiler plugin that supports annotation processing. In the dependencies 
section, write the following (on three lines; remove the newlines after implementation and 
kapt):
// Room
implementation
"android.arch.persistence.room:runtime:1.0.0"
kapt
"android.arch.persistence.room:compiler:1.0.0"
Room Architecture
Room was designed with ease of use in mind; you basically deal with three kinds of objects.

Database: Represents a holder for the database. Talking in SQL 
language idioms, it contains several tables. To say it in a technology-
agnostic way, a database contains several entity containers.

Entity: Represents a table in the SQL world. To say it in a technology-
agnostic way, this is a usage-centric aggregate of fields. An example 
would be an employee inside a company or a contact holding 
information about how to communicate with people or partners.

Data Access Object (DAO): Contains the access logic to retrieve data 
from the database. It thus serves as an interface between the program 
logic and the database model. You often have one DAO per entity 
class but possibly more DAOs for various combinations. You could, for 
example, have an EmployeeDao and a ContactDao for the two employee 
and contact entities, as well as a PersonDao that combines the employee 
and the contacts information of a person.
The Database
To declare a database, you write the following:
import android.arch.persistence.room.*
@Database(entities =
arrayOf(Employee::class, Contact::class),
version = 1)
abstract class MyDatabase : RoomDatabase() {
abstract fun employeeDao(): EmployeeDao



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   99   100   101   102   103   104   105   106   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling