Lets say I have a factory that retrieves an instan...
# getting-started
m
Lets say I have a factory that retrieves an instance of a model based on a reference. Lets say I have a bunch of values I need to either: Store into a new class if its reference does not exist, or modify on an existing one if it does exist. Now I need to constantly redeclare the mandatory fields in every single one of my models.
Copy code
fun main(){
    val f1: Int = 3
    val f2: String = "foo"
    val f3: Double = 23.2
    val f4: String = "bar"
    val f5: String? = "rez"
    val f6: String? = null

    val model = DBModelRepository.getByField1(f1)
        ?.apply {
            field2 = f2     //need to doubly write all these mandatory fields
            field3 = f3     //
            field4 = f4     //
        } ?: DBModel(
            field1 = f1,    //
            field2 = f2,    //
            field3 = f3,    //
            field4 = f4,    //can become a big hassle when its 15+ fields
        )
    model.apply {
        field5 = f5         //I can at least save a bit of hassle by doing the optional fields after both cases
        field6 = f6
    }
    DBModelRepository.save(model)
}

class DBModel(
    val field1: Int,
    var field2: String,
    var field3: Double,
    var field4: String,
    var field5: String? = null,
    var field6: String? = null
)

object DBModelRepository{
    fun getByField1(field1: Int) : DBModel? {
        TODO()
    }
    fun save(dbMobel: DBModel){}
}
Now is this a relatively "small example" with 4 fields, but we're working with a lot of pretty big models. Sometimes its a bit harder than just "updating" a model like the example and we need to redo this logic in other places. Is there a better way to do this?
a
Hi @Michael de Kaste, kindly refer to following code, i think it fits your requirement, i'm using Object Declaration on DBModel, based on the example below i hope you can see and understand how to optimize your code,Hope this helps
Copy code
fun main(){

    val model = DBModelRepository.getByField1(DBModel.field1)
        ?.apply {
                //need to doubly write all these mandatory fields
            DBModel.field3=23.2    //
            DBModel.field4="bar"     //
        }

    model.apply {
                //I can at least save a bit of hassle by doing the optional fields after both cases

    }
    DBModelRepository.save(model)
}

object DBModel{
    var field1: Int=0
    var field2: String=""
    var field3: Double=0.0
    var field4: String=""
    var field5: String? = null
    var field6: String? = null
}

object DBModelRepository{
    fun getByField1(field1: Int) : DBModel {
        TODO()
    }
    fun save(dbMobel: DBModel){}
}
The following portion i omitted it as i'm not certain what its for
Copy code
?: DBModel(
            field1 = f1,    //
            field2 = f2,    //
            field3 = f3,    //
            field4 = f4,    //can become a big hassle when its 15+ fields
        )
So now you only have to create a skeleton using the Object declaration and you can use it through your code without having to repeat declaring the models
m
Basic values of the database model are not a thing. The String, Int, etc. Fields are examples, but should never be filled with a basic value, because thats not an accepted value. The types ares more complex, and cant be prefilled.
a
Could you show an example of what those complex type looks like?
n
This looks like a variation of the SQL "upsert" issue: either insert or update a row. I think what i would try is to always first create a new object and then have a
fun DBModel?.merge(update: DBModel)
method. This IMHO makes the code more readable even if you don't have multiple "upsert" calls for the same model.
Copy code
val model = DBModelRepository.getByField1(f1).merge(
        DBModel(
            field1 = f1,
            field2 = f2,
            field3 = f3,
            field4 = f4,
            field5 = f5,
            field6 = f6,
        )
    )

fun DBModel?.merge(update: DBModel) = this?.apply {
    field2 = update.field2
    field3 = update.field3
    field4 = update.field4
    field5 = update.field5
    field6 = update.field6
} ?: update