4 Creata a Dao
Marcel van der Heide edited this page 2023-01-20 15:26:39 +01:00

Creating a Dao

Creating a Dao with RoomEx is almost identical with the creation of a Dao in Android Room. There are some requirements which need to be fulfilled for a Dao to be valid:

  1. Class must implement RoomExDao<T>.
  2. Class has to be annotated with @DaoEx, not @Dao.
  3. There has to be one, and only one Dao for each Entity. (If no Dao is needed, let RoomEx create one.)

Compared to a standard Dao from Room, there are no other restrictions.

Example:

@DaoEx
abstract class UserDao : RoomExDao<User>() {

    @Query("SELECT * FROM User WHERE firstName = 'John' ")
    abstract suspend fun findAllJohns() : MutableList<User>
}

Note: We can also let RoomEx autmatically generate a Dao.

Methods

As you can see in the example above, Room annotations and convenience methods can be used and will be automatically implemented by Room. These methods include:

@Insert
suspend fun insert(entity: T) : Long
@Update
suspend fun update(entity: T) : Int
@Delete
suspend fun delete(entity: T) : Int
@Query("SELECT * FROM User WHERE id = :id")
public abstract fun getById(id: Long): List<User>

For convenience, these methods, among others, are already implemented in the RoomExDao<T> class, as protected functions and can be called from your Dao if needed. A complete list of all available methods can be seen here: Dao methods.
In order for RoomEx to automatically implement these, or any subset of these methods, publically for your Dao, simply add one, or multiple of the following interfaces:

CRUD interfaces

When using any of the CRUD interfaces for your Dao, RoomEx will automatically implement the methods for you, making the protected RoomExDao<T> methods public. A complete description of these methods can be found here: Dao methods.

ICrud<T>

The ICrud<T> interface combines all the other CRUD interfaces in a single interface.

interface ICrud<T> : ICreate<T>, IRead<T>, IUpdate<T>, IDelete<T>, IJoins<T> where T : RoomExEntity

ICreate<T>

The ICreate<T> interface exposes methods for inserting new entities in the local database.

interface ICreate<T> where T : RoomExEntity {

    @Insert
    suspend fun insert(entity: T): Long
    
    suspend fun insertWithChildren(entity: T, recursively: Boolean = false): Long
    
    suspend fun insertWithChildren(entity: T, recursively: Boolean = false, vararg fields: String): Long
}

IRead<T>

The IRead<T> interface exposes methods for reading existing entities from the local database.

interface IRead<T> where T : RoomExEntity {

    suspend fun getById(id: Long, useResolver: Boolean = true): T?

    suspend fun getAll(): MutableList<T>

    suspend fun getByIdWithChildren(id: Long, useResolver: Boolean = true, recursively: Boolean = false, vararg fields: String) : T?

    suspend fun loadChildren(entity: T, useResolver: Boolean = true, recursively: Boolean = false) : T

    suspend fun loadChildren(entity: T, useResolver: Boolean = true, recursively: Boolean = false, vararg fields : String) : T
}

IUpdate<T>

The IUpdate<T> interface exposes methods for updating existing entities in the local database.

interface IUpdate<T> where T : RoomExEntity {

    @Update
    suspend fun update(entity: T)
    
    suspend fun updateWithChildren(entity: T, removeIfNull: Boolean = false, removeFromList: Boolean = true, recursively: Boolean = false)
    
    suspend fun updateWithChildren(entity: T, removeIfNull: Boolean = false, removeFromList: Boolean = true, recursively: Boolean = false, vararg fields: String)
}

IDelete<T>

The IDelete<T> interface exposes methods for deleting existing entities from the local database.

interface IDelete<T> where T : RoomExEntity {

    @Delete
    suspend fun delete(entity: T): Int

    suspend fun deleteWithChildren(entity: T, recursively: Boolean = false, vararg fields : String): Int

    suspend fun deleteChildren(entity: T, recursively: Boolean = false, clearFromEntity: Boolean = false, vararg fields : String)
}

IJoins<T>

The IJoins<T> interface exposes methods to add or remove joins between entities in the local database.

interface IJoins<T> where T : RoomExEntity {

    suspend fun addJoin(entity: T, child: RoomExEntity, fieldName: String)

    suspend fun addJoins(entity: T, children: List<RoomExEntity>, fieldName: String)

    suspend fun removeJoin(entity: T, child: RoomExEntity, fieldName: String)

    suspend fun removeJoins(entity: T, children: List<RoomExEntity>, fieldName: String)

    suspend fun removeJoinsIfNull(entity: T, vararg fields : String)
}

IResolver<T>

The ICrud<T> interface exposes methods for resolve entities from other sources.

interface IResolver<T> where T : RoomExEntity {

    fun addResolver(resolver: IStorageResolver) : Boolean

    fun removeResolver(resolver: IStorageResolver) : Boolean

    fun storeResolvedEntitiesLocally(value: Boolean?): Boolean
}