Last active
September 3, 2019 22:44
-
-
Save DenisBronx/6c441a4119c1e50835db3fdca4106f5a to your computer and use it in GitHub Desktop.
Repository pattern gists
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
// A wrapper for handling failing requests | |
sealed class Result<T> { | |
data class Success<T>(val value: T) : Result<T>() | |
data class Failure<T>(val throwable: Throwable) : Result<T>() | |
} | |
// A DataStore for the SharedPreferences | |
interface ProductPreferences { | |
fun isFavourite(id: String?): Boolean | |
} | |
// A DataStore for the Remote DB | |
interface ProductApiService { | |
fun getProducts(): Single<Result<List<NetworkProduct>>> | |
fun getWishlist(productIds: List<String>): Single<Result<List<NetworkProduct>>> | |
} | |
// A cluster of DTOs to be mapped into a Product | |
data class DataProduct( | |
val networkProduct: NetworkProduct, | |
val isFavourite: Boolean | |
) |
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
// Database DTO | |
@Entity(tableName = "Product") | |
data class DBProduct( | |
@PrimaryKey | |
@ColumnInfo(name = "id") | |
val id: String, | |
@ColumnInfo(name = "name") | |
val name: String, | |
@ColumnInfo(name = "nowPrice") | |
val nowPrice: Double, | |
@ColumnInfo(name = "wasPrice") | |
val wasPrice: Double | |
) |
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
// Non-nullable to Non-nullable | |
interface ListMapper: Mapper<List<I>, List<O>> | |
class ListMapperImpl<I, O>( | |
private val mapper: Mapper<I, O> | |
) : ListMapper<I, O> { | |
override fun map(input: List<I>): List<O> { | |
return input.map { mapper.map(it) } | |
} | |
} |
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 Mapper<I, O> { | |
fun map(input: I): O | |
} |
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
// Network DTO | |
data class NetworkProduct( | |
@SerializedName("id") | |
val id: String?, | |
@SerializedName("name") | |
val name: String?, | |
@SerializedName("nowPrice") | |
val nowPrice: Double?, | |
@SerializedName("wasPrice") | |
val wasPrice: Double? | |
) |
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
// Nullable to Non-nullable | |
interface NullableInputListMapper: Mapper<List<I>?, List<O>> | |
class NullableInputListMapperImpl<I, O>( | |
private val mapper: Mapper<I, O> | |
) : NullableInputListMapper<I, O> { | |
override fun map(input: List<I>?): List<O> { | |
return input?.map { mapper.map(it) }.orEmpty() | |
} | |
} |
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
// Non-nullable to Nullable | |
interface NullableOutputListMapper: Mapper<List<I>, List<O>?> | |
class NullableOutputListMapperImpl<I, O>( | |
private val mapper: Mapper<I, O> | |
) : NullableOutputListMapper<I, O> { | |
override fun map(input: List<I>): List<O>? { | |
return if (input.isEmpty()) null else input.map { mapper.map(it) } | |
} | |
} |
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
// Entity | |
data class Product( | |
val id: String, | |
val name: String, | |
val price: Price | |
) { | |
// Value object | |
data class Price( | |
val nowPrice: Double, | |
val wasPrice: Double | |
) { | |
companion object { | |
val EMPTY = Price(0.0, 0.0) | |
} | |
} | |
} |
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
@Entity(tableName = "Product") | |
data class ProductDTO( | |
@PrimaryKey | |
@ColumnInfo(name = "id") | |
@SerializedName("id") | |
val id: String?, | |
@ColumnInfo(name = "name") | |
@SerializedName("name") | |
val name: String?, | |
@ColumnInfo(name = "nowPrice") | |
@SerializedName("nowPrice") | |
val nowPrice: Double?, | |
@ColumnInfo(name = "wasPrice") | |
@SerializedName("wasPrice") | |
val wasPrice: Double? | |
) |
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
class ProductRepositoryImpl( | |
private val productApiService: ProductApiService, | |
private val productDataMapper: Mapper<DataProduct, Product>, | |
private val productPreferences: ProductPreferences | |
) : ProductRepository { | |
override fun getWishlist(): Single<Result<List<Product>>> { | |
return productApiService.getWishlist(productPreferences.getFavourites()).map { | |
when (it) { | |
is Result.Success -> Result.Success(mapWishlist(it.value)) | |
is Result.Failure -> Result.Failure<List<Product>>(it.throwable) | |
} | |
} | |
} | |
private fun mapWishlist(wishlist: List<NetworkProduct>): List<Product> { | |
return wishlist.map { | |
productDataMapper.map(DataProduct(it, true)) | |
} | |
} | |
} |
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
class ProductRepositoryImpl( | |
private val productApiService: ProductApiService, | |
private val productDataMapper: Mapper<DataProduct, Product>, | |
private val productPreferences: ProductPreferences | |
) : ProductRepository { | |
override fun getProducts(): Single<Result<List<Product>>> { | |
return productApiService.getProducts().map { | |
when(it) { | |
is Result.Success -> Result.Success(mapProducts(it.value)) | |
is Result.Failure -> Result.Failure<List<Product>>(it.throwable) | |
} | |
} | |
} | |
private fun mapProducts(networkProductList: List<NetworkProduct>): List<Product> { | |
return networkProductList.map { | |
productDataMapper.map(DataProduct(it, productPreferences.isFavourite(it.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
// Entity | |
data class Product( | |
val id: String, | |
val name: String, | |
val price: Price, | |
val isFavourite: Boolean | |
) { | |
// Value object | |
data class Price( | |
val nowPrice: Double, | |
val wasPrice: Double | |
) { | |
companion object { | |
val EMPTY = Price(0.0, 0.0) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment