Skip to content

Instantly share code, notes, and snippets.

@isyufu
Created January 17, 2019 00:28
Show Gist options
  • Save isyufu/c8419b80d162eb5bccf4b64c3912d5b0 to your computer and use it in GitHub Desktop.
Save isyufu/c8419b80d162eb5bccf4b64c3912d5b0 to your computer and use it in GitHub Desktop.
kt getUserFromDatabase getOrElse FromNetwork
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