我正在https://github.com/antoniolg/Kotlin-for-Android-Developers
学习“Kotlin for Android Developers(书籍)”的示例在不同的kt文件中有三个类,我认为这三个类是相似的,而这些多个类使程序变得复杂。
如何重新设计项目框架并使项目更清晰?
DbClasses.kt
class DayForecast(var map: MutableMap<String, Any?>) {
var _id: Long by map
var date: Long by map
var description: String by map
var high: Int by map
var low: Int by map
var iconUrl: String by map
var cityId: Long by map
constructor(date: Long, description: String, high: Int, low: Int, iconUrl: String, cityId: Long)
: this(HashMap()) {
this.date = date
this.description = description
this.high = high
this.low = low
this.iconUrl = iconUrl
this.cityId = cityId
}
}
tables.kt
object DayForecastTable {
val NAME = "DayForecast"
val ID = "_id"
val DATE = "date"
val DESCRIPTION = "description"
val HIGH = "high"
val LOW = "low"
val ICON_URL = "iconUrl"
val CITY_ID = "cityId"
}
DomainClasses.kt
data class Forecast(
val id: Long,
val date: Long,
val description: String,
val high: Int,
val low: Int,
val iconUrl: String
)
答案 0 :(得分:1)
就数据库相关类而言,您可以考虑使用ORM库来注释字段并生成数据库表模式(删除对DayForecastTable的需求),例如:房间(https://developer.android.com/training/data-storage/room/index.html)
您可以在整个应用程序中使用这些类来减少对DomainClasses的需求,尽管我建议保留Domain层类以使域模型独立于数据库。