Last active
October 14, 2022 02:57
-
-
Save GabrielBrasileiro/7dbe145955dafb694d8d08f980d798c5 to your computer and use it in GitHub Desktop.
Koin extensions to solve type erasure of generic mappers
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
/** | |
* This will recover and inject your interface in your fragments or activities | |
*/ | |
inline fun <reified I, reified O> ComponentCallbacks.injectMapper(): Lazy<Mapper<I, O>> { | |
return inject(named(identifier<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
/** | |
* Activity, Fragment and KoinComponent usage | |
* | |
* Figurative class declaration | |
*/ | |
class Component: KoinComponent, Activity(), Fragment() { | |
// Here we can recover the instance of mapper and use. :) | |
private val mapper by injectMapper<Int, String>() | |
} |
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
/** | |
* This method indentifies the input and output returning a string. | |
*/ | |
inline fun <reified I, reified O> identifier(): String { | |
val inputIdentifier = I::class.java.`package`.name + I::class.java.name | |
val outputIdentifier = O::class.java.`package`.name + O::class.java.name | |
return inputIdentifier + outputIdentifier | |
} |
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
/** | |
* This method will declare your implementation inside module lambda | |
*/ | |
inline fun <reified I, reified O> Module.mapper( | |
noinline definition: Definition<Mapper<I, O>> | |
): BeanDefinition<Mapper<I, O>> { | |
return factory(qualifier = named(identifier<I, O>()), definition = definition) | |
} | |
/** | |
* This will recover and inject your interface like the method get() in koin. | |
*/ | |
inline fun <reified I, reified O> Scope.getMapper(): Mapper<I, O> { | |
return get(named(identifier<I, O>())) | |
} | |
/** | |
* This will recover and inject your interface in implementations of KoinComponent | |
*/ | |
inline fun <reified I, reified O> KoinComponent.injectMapper(): Lazy<Mapper<I, O>> { | |
return inject(named(identifier<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
/** | |
* This interface defines the control of input and output objects. | |
*/ | |
interface Mapper<I, O> { | |
fun map(enter: 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
/** | |
* Module usage | |
*/ | |
module { | |
// Here is the implementation of mapper Mapper<PersonData, Person>. | |
mapper { PeopleMapper() } | |
// ... Declarations ... | |
// The param in method is Mapper<PersonData, Person>. | |
mapper { PeoplePageMapper(getMapper()) } | |
factory<PeopleRepository> { | |
// getMapper() recover the mapper and inject. | |
PeopleRepositoryImpl(get(), get(), getMapper(), getMapper()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice