Android Clean Architecture : Cache Mapping When Model refers to two room tables

823 views Asked by At

I have a query which joins two tables together. I created a data class in kotlin which contains the vals from each table I need returned by the query. When I update the model in the DAO query I obviously have to go through the layers of the clean arch updating the model class. I got to the Mapper class which maps the entities to the domain layer. Here I have run into an issue because the function can only extend to one room entity and my dataclass references values from two tables.

Interface

interface EntityMapper <Entity, DomainModel>{

fun mapFromEntity(entity: Entity): DomainModel
fun mapToEntity(domainModel: DomainModel): Entity
}

Class

 fun entityListToList(entityTableA: List<TableA>): List<Model> {
    val list: ArrayList<Model> = ArrayList()
    for (entity in entityTableA) {
        list.add(mapFromEntity(entity))
    }
    return list
}


fun listToEntityList(entityTableA: List<Model>): List<TableA> {
    val entity: ArrayList<TableA> = ArrayList()
    for (entity in entityTableA) {
        entity.add(mapToEntity(entityTableA))
    }
    return entityTableA
}


override fun mapFromEntity(entityTableA: TableA): Model{
    return Model()
}


 override fun mapToEntity(domainModel: Model): TableA{
    return TableA
}

So this function is missing all the values from table B as I cant extend both tables in the mapper function. How do people work around this as presumably its possible to run joined queries in clean architecture otherwise it would be fairly pointless in any app that isnt simplistic.

1

There are 1 answers

4
Mohsen On BEST ANSWER

you haven't provided enough code but I'm guessing your mapper looks something like this:

interface Mapper<F, S> {
    fun firstToSecond(first: F): S
    fun secondToFirst(second: S): F
}

then if you need to map one class to two separate classes you need to use a Pair.

class MyMapper<CustomModel, Pair<TableA, TableB> {
    override fun mapToEntity(domainModel: CustomModel): Pair<TableA, TableB>{
        ...
}