Created
February 23, 2023 15:14
-
-
Save jeroenr/c08a01eb07b64de499ec6f10a9cdb2f3 to your computer and use it in GitHub Desktop.
Example of a port and an adapter
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
// in Domain Module | |
data class ExchangeRateDto(val rate: BigDecimal, val currency: Currency) | |
interface ExchangeRateApiClientPort { | |
fun getRate(source: Currency, target: Currency): ExchangeRateDto | |
} | |
// in Infrastructure Module | |
class ExchangeRateApiClientAdapter : ExchangeRateApiClientPort { | |
override fun getRate(source: Currency, target: Currency): ExchangeRateDto { | |
val rate = MonetaryConversions | |
.getConversion(source.toString()) | |
.getExchangeRate(moneta.Money.of(1, target.toString())) | |
return ExchangeRateDto( | |
rate.factor.numberValueExact(BigDecimal::class.java), | |
Currency.valueOf(rate.currency.currencyCode) | |
) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment