Skip to content

Instantly share code, notes, and snippets.

@jeroenr
Created February 23, 2023 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroenr/c08a01eb07b64de499ec6f10a9cdb2f3 to your computer and use it in GitHub Desktop.
Save jeroenr/c08a01eb07b64de499ec6f10a9cdb2f3 to your computer and use it in GitHub Desktop.
Example of a port and an adapter
// 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