Created
February 1, 2019 02:14
-
-
Save wada811/1fde4ae54d3a2a3ac755323adaac3dc2 to your computer and use it in GitHub Desktop.
PhantomType で DB 保存前後の nullable な id を撲滅しつつオブジェクトのプロパティは共通化する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class State { | |
object NotSaved : State() | |
object Saved : State() | |
} | |
sealed class SessionDataModel<S : State> { | |
abstract val title: String | |
abstract val isLiked: Boolean | |
fun toggleLike() { | |
isLiked != isLiked | |
} | |
class SessionRawDataModel( | |
override val title: String, | |
override val isLiked: Boolean | |
) : SessionDataModel<State.NotSaved>() { | |
fun save(id: SessionId): SessionSavedDataModel = SessionSavedDataModel(id, title, isLiked) | |
} | |
class SessionSavedDataModel( | |
val id: SessionId, | |
override val title: String, | |
override val isLiked: Boolean | |
) : SessionDataModel<State.Saved>() | |
} | |
// application layer | |
fun save(sessionRawData: SessionDataModel.SessionRawDataModel) { | |
val id = insertToDB(sessionRawData) | |
val sessionSavedData: SessionDataModel.SessionSavedDataModel = sessionRawData.save(id) | |
} | |
// infra layer | |
fun insertToDB(sessionRawData: SessionDataModel.SessionRawDataModel): SessionId = TODO("DB にインサートして last insert id を返す") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment