Hey guys, I have a data class `Advertisement` whic...
# android
o
Hey guys, I have a data class
Advertisement
which contains a few enums.
Copy code
@Parcelize
data class Advertisement(
    @SerializedName("type") val type : AdvertisementType,
    @SerializedName("userTypeTarget") val userTypeTarget : UserTypeTarget,
    @SerializedName("goal") val advertisementGoal : AdvertisementGoal,
    @SerializedName("attachTo") val attachTo : AttachTo,
) : Parcelable

enum class AdvertisementType(private val value : Int) {
    REGULAR(0),
    SOS(1)
}

enum class AttachTo(private val value : Int){
    ME(0),
    MY_OFFICE(1)
}

enum class UserTypeTarget(private val value : Int){
    LAWYER(0),
    SERVICE_PROVIDER(1)
}

enum class AdvertisementGoal(private val value : Int){
    COLLABORATION(0),
    CASE_TRANSFER(1),
    COURT_TRANSFER(2),
    FORM_VALIDATION(3)
}
As you can see, my object properties are enums. However, our backend only accepts Integers. I need to make all the properties into Integers, but I don’t want any future developer to ever create an instance of`Advertisement` directly with some Int, and use the created enums instead. How can I force the insertion of enums yet the actual value of created `Advertisement`to be an Int?
s
Have distinct business models and REST models. You’ll have
RestAdvertisement
which can be mapped from/to an
Advertisement
👍 1
o
@satyan It will work, but isn’t it code duplication? why is this a good practice to separate them? Because following this logic I might need to do it with many other objects in the project
s
It’s not code duplication. Your business model isn’t supposed to be exactly the same as your REST model. You’ll express business logic using business models while REST model would only be raw models to send to the back-end
o
Thank you @satyan I will consider refactoring to your approach. Regardless, how about doing something like this?
Copy code
val type : AdvertisementType
get() = type.toInt() // pseudo code, but I wonder if such thing possible
This way I set enum but get Int I guess. Is there a possible wrong thing with that approach I might havn’t think of?