Created
August 30, 2020 21:45
-
-
Save bloderxd/db23d7c7f6a926be9442b654cfdd951f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
interface Mapper<P, V> { | |
fun P.toValue(): V | |
fun V.toPayload(): P | |
} |
This file contains hidden or 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
interface Repository<P, T> { | |
suspend fun P.load(): T | |
} |
This file contains hidden or 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
suspend fun <T, P, R, M> T.fetchMapping(p: P): M where T : Repository<P, R>, T : Mapper<R, M> = p.load().mapToValue() |
This file contains hidden or 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
object UserBalanceMapper : Mapper<UserBalancePayload, UserBalance> { | |
override suspend fun UserBalancePayload.toValue(): UserBalance = UserBalance(balance) | |
override suspend fun UserBalance.toPayload(): UserBalancePayload = UserBalancePayload(balance) | |
} |
This file contains hidden or 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
data class UserBalanceParams(val userId: String) | |
object UserBalanceRepository : Repository<UserBalanceParams, UserBalancePayload> { | |
override suspend fun UserBalanceParams.load(): UserBalancePayload = UserBalancePayload(100.0) | |
} |
This file contains hidden or 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
class UserDescriptionUseCase( | |
private val repository: Repository<UserBalanceParams, UserBalancePayload> = UserBalanceRepository, | |
private val mapper: Mapper<UserBalancePayload, UserBalance> = UserBalanceMapper | |
) : Repository<UserBalanceParams, UserBalancePayload> by repository, Mapper<UserBalancePayload, UserBalance> by mapper { | |
suspend operator fun invoke(userId: String) = fetchMapping(UserDescriptionParams(userId)) | |
} |
This file contains hidden or 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
object UserDescriptionMapper : Mapper<UserDescriptionPayload, UserDescription> { | |
override suspend fun UserDescriptionPayload.toValue(): UserDescription = UserDescription(name, age) | |
override suspend fun UserDescription.toPayload(): UserDescriptionPayload = UserDescriptionPayload(name, age) | |
} |
This file contains hidden or 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
data class UserDescriptionParams(val userId: String) | |
object UserDescriptionRepository : Repository<UserDescriptionParams, UserDescriptionPayload> { | |
override suspend fun UserDescriptionParams.load(): UserDescriptionPayload = UserDescriptionPayload("Bloder", 22) | |
} |
This file contains hidden or 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
class UserDescriptionUseCase( | |
private val repository: Repository<UserDescriptionParams, UserDescriptionPayload> = UserDescriptionRepository, | |
private val mapper: Mapper<UserDescriptionPayload, UserDescription> = UserDescriptionMapper | |
) : Repository<UserDescriptionParams, UserDescriptionPayload> by repository, Mapper<UserDescriptionPayload, UserDescription> by mapper { | |
suspend operator fun invoke(userId: String) = fetchMapping(UserDescriptionParams(userId)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment