Created
January 17, 2019 00:28
-
-
Save isyufu/c8419b80d162eb5bccf4b64c3912d5b0 to your computer and use it in GitHub Desktop.
kt getUserFromDatabase getOrElse FromNetwork
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
interface DaoOperationsSyntax { | |
val dao: DaoDatabase | |
fun User.queryUser() = | |
dao.query("SELECT * from Users where userId = ${this.id}") | |
fun Company.queryCompany() = | |
dao.query("SELECT * from Companies where companyId = ${this.id}") | |
} | |
//We can also combine them together to form new types. Let's create a new NetworkSyntax interface. | |
interface NetworkSyntax { | |
val network: NetworkModule | |
fun User.requestUser() = | |
network.request(this.id) | |
} | |
//And an interface that's a combination of Network + Dao: | |
interface RequestSyntax: DaoOperationsSyntax, NetworkSyntax { | |
fun User.fetchUser() = | |
Try { queryUser() }.getOrElse { requestUser() } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment