trait TradeRepository { def fetch(refNo: String): Trade def write(t: Trade): Unit}
trait TradeRepositoryComponent { val tradeRepo: TradeRepository class TradeRepositoryImpl extends TradeRepository { def fetch(refNo: String): Trade = //codes def write(t: Trade): Unit = //codes }}
trait TradeService { def fetchTrade(refNo: String): Trade def writeTrade(trade: Trade): Unit } trait TradeServiceComponent { this: TradeRepositoryComponent => //Self typing! val tradeService: TradeService class TradeServiceImpl extends TradeService { def fetchTrade(refNo: String) = tradeRepo.fetch(refNo) def writeTrade(trade: Trade) = tradeRep.write(trade) } }
object TradeServiceAssembly extends TradeRepositoryComponent with TradeServiceComponent { val tradeRepo = new TradeRepositoryImpl val tradeService = new TradeServiceImpl } import TradeServiceAssembly._ val trade = tradeService.fetchTrade("2938") tradeService.writeTrade(t)
val mockTrader = new TradeRepositoryComponent with TradeServiceComponent { val tradeRepo = new TradeRepositoryMock val tradeService = new TradeServiceMock } import mockTrader._ val trade = tradeService.fetchTrade("2983") tradeService.writeTrade(trade)
trait TradeRepository { def fetch(refNo: String): Trade def update(trade: Trade): Trade def write(trade: Trade): Unit}
trait TradeService { val fetchTrade: TradeRepository => String => Trade = {repo => refNo => repo.fetch(refNo)} val updateTrade: TradeRepository => Trade => Trade = {repo => trade => repo.update(trade)} val writeTrade: TradeRepository => Trade => Unit = {repo => trade => repo.write(trade)} }
trait TradeRepositoryRedis extends TradeRepository { def fetch(refNo: String): Trade = redis//... def update(trade: Trade): Trade = redis//... def write(trade: Trade): Trade = redis//... }
object TradeServiceRedisContext extends TradeService { val fetchTrade_curried = fetchTrade(new TradeRepositoryRedis) val updateTrade_curried = updateTrade(new TradeRepositoryRedis) val writeTrade_curried = writeTrade(new TradeRepositoryRedis)}
//WAS val fetchTrade: TradeRepository => String => Trade = {repo => refNo => repo.fetch(refNo)} //Curried val fetchTrade_curried: String => Trade = fetchTrade(new TradeRepositoryRedis)
import TradeServiceRedisContext._ //Imports val trade = fetchTrade_curried("2983") writeTrade_curried(trade)