December 2018
Intermediate to advanced
414 pages
10h 19m
English
The first way to do DI is to pass the collaborators in the constructor, where they are then saved in private properties. Let's have as an example on e-commerce app, whose Basket is handled both locally and remotely. The BasketClient class orchestrates the logic, saves locally in BasketStore, and synchronizes remotely with BasketService:
protocol BasketStore { func loadAllProduct() -> [Product] func add(product: Product) func delete(product: Product)}protocol BasketService { func fetchAllProduct(onSuccess: ([Product]) -> Void) func append(product: Product) func remove(product: Product)}struct Product { let id: String let name: String //...}
Then in the constructor of BasketClient, the concrete implementations of the protocols ...
Read now
Unlock full access