Share
Embed
Dependency Injection Pattern for iOS App
Mobilization 2018 in Lodz, 20 October, 2018
http://2018.mobilization.pl/
Yoichi Tagaya
October 20, 2018
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
47 slides
-
!ZPJDIJUHZ About me as Swinject author • Swinject: dependency injection
framework for Swift • Maintained by 5 members • Recently by Jakub Vano especially! • 2,7k+ GitHub stars now and 1k clones everyday • 4 extensions provided -
!ZPJDIJUHZ Agenda • Software architecture • Dependency injection (DI) •
Swinject (DI container) -
!ZPJDIJUHZ Peter Eeles says An architecture • Defines structure and
behavior • Focuses on significant elements • May conform to an architectural style • Influences team structure (inverse Conway maneuver) • Balances stakeholder needs • Is influenced by its environment • Embodies decisions based on rationale -
!ZPJDIJUHZ Architecture is not static but dynamic .7$ "SDIJUFDUVSF :FBST
TJODFMBVODI .77. 7*1&3 &YBNQMFPGBQSPEVDU 3*#T &OHJOFFST 6TFST L . . -
!ZPJDIJUHZ Architecture is not static but dynamic $ # "
/PX $ /FX# " $ % -BUFS -
!ZPJDIJUHZ Production / unit test $ # " 1SPEVDUJPO 6OJU5FTU
.PDLTUVC PG# " -
!ZPJDIJUHZ SOLID principles • S - Single responsibility principle •
O - Open/closed principle • L - Liskov substitution principle • I - Interface segregation principle • D - Dependency inversion principle • Should not depend on concretions but abstractions -
!ZPJDIJUHZ Depending on concretions / abstractions Depending on concretions (tight
coupling) *NQMFNFOUBUJPO $PODSFUF5ZQF Depending on abstractions (loose coupling) 3FGFSFODF *NQMFNFOUBUJPO Protocol (Interface) 3FGFSFODF $PODSFUF5ZQF" $PODSFUF5ZQF# -
!ZPJDIJUHZ Dependency injection: a way to realize dependency inversion principle
*NQMFNFOUBUJPO Protocol (Interface) 3FGFSFODF *OTUBODFPG $PODSFUF5ZQF# 1BTT JOKFDU UPJOJUJBMJ[FS -
!ZPJDIJUHZ Dependency injection: a way to realize dependency inversion principle
*NQMFNFOUBUJPO Protocol (Interface) 3FGFSFODF $PODSFUF5ZQF# 5IFTZTUFNJTDPOpHVSFE UPVTF$PODSFUF5ZQF# -
!ZPJDIJUHZ Code example of dependency injection protocol APIClientProtocol { ...
} class APIClient: APIClientProtocol { ... } let vm = MyViewModel(apiClient: APIClient()) class MyViewModel { let apiClient: APIClientProtocol init(apiClient: APIClientProtocol) { self.apiClient = apiClient } } APIClientProtocol "CTUSBDUUZQF *OKFDUDPODSFUFUZQF -
!ZPJDIJUHZ Code example of registration $POUBJOFS Register import Swinject let
container = Container() container.register(MyViewModel.self) { resolver in let apiClient = resolver.resolve(APIClientProtocol.self)! return MyViewModel(apiClient: apiClient) } container.register(APIClientProtocol.self) { _ in APIClient() } -
!ZPJDIJUHZ Code example of resolution $POUBJOFS Resolve import Swinject let
vm = container.resolve(MyViewModel.self)! -
!ZPJDIJUHZ In a production app class MyViewModel { init(dep1: D1,
dep2: D2, dep3: D3, id: String, name: String) { ... } } .BOZEFQFOEFODJFT 3VOUJNFQBSBNFUFST PUIFSUIBOEFQFOEFODJFT %FQFOEFODZJOKFDUJPODPEFJTPGUFOMJLFUIJT -
!ZPJDIJUHZ Using typealias of dependency tuple init(dependency: Dependency, id: String,
name: String) 5ZQFBMJBTPGUVQMF % % % %FQFOEFODZ 1BSBNFUFST -
!ZPJDIJUHZ Example of class definition with typealias of tuple protocol
APIClientProtocol { ... } class APIClient: APIClientProtocol { ... } protocol LoggerProtocol { ... } class Logger: LoggerProtocol { ... } 5ZQFBMJBTPG EFQFOEFODZUVQMF class MyViewModel { typealias Dependency = ( APIClientProtocol, LoggerProtocol ) let (apiClient, logger): Dependency let id: String init(dependency: Dependency, id: String) { (apiClient, logger) = dependency self.id = id } } &YBNQMFXJUIEFQFOEFODJFTBOEQBSBNFUFS -
!ZPJDIJUHZ DI Registration let container = Container() container.register(APIClientProtocol.self) { _
in APIClient() } container.register(LoggerProtocol.self) { _ in Logger() } $POUBJOFS Register container.register(MyViewModel.self) { (resolver, id: String) in let dependency = ( resolver.resolve(APIClientProtocol.self)!, resolver.resolve(LoggerProtocol.self)! ) return MyViewModel( dependency: dependency, id: id ) } -
!ZPJDIJUHZ Add another dependency let (apiClient, logger): Dependency let id:
String init(dependency: Dependency, id: String) { (apiClient, logger) = dependency self.id = id } } class MyViewModel { typealias Dependency = ( APIClientProtocol, LoggerProtocol ) -
!ZPJDIJUHZ Add another dependency protocol DatabaseProtocol { ... } class
Database: DatabaseProtocol { ... } let (apiClient, logger): Dependency let id: String init(dependency: Dependency, id: String) { (apiClient, logger) = dependency self.id = id } } class MyViewModel { typealias Dependency = ( APIClientProtocol, LoggerProtocol ) -
!ZPJDIJUHZ Add another dependency protocol DatabaseProtocol { ... } class
Database: DatabaseProtocol { ... } class MyViewModel { typealias Dependency = ( APIClientProtocol, LoggerProtocol, DatabaseProtocol ) let (apiClient, logger): Dependency let id: String init(dependency: Dependency, id: String) { (apiClient, logger) = dependency self.id = id } } -
!ZPJDIJUHZ Add another dependency protocol DatabaseProtocol { ... } class
Database: DatabaseProtocol { ... } class MyViewModel { typealias Dependency = ( APIClientProtocol, LoggerProtocol, DatabaseProtocol ) let (apiClient, logger): Dependency let id: String init(dependency: Dependency, id: String) { (apiClient, logger) = dependency self.id = id } } 8SPOHUVQMFIFSF ❕ -
!ZPJDIJUHZ Add another dependency protocol DatabaseProtocol { ... } class
Database: DatabaseProtocol { ... } class MyViewModel { typealias Dependency = ( APIClientProtocol, LoggerProtocol, DatabaseProtocol ) let (apiClient, logger, database): Dependency let id: String init(dependency: Dependency, id: String) { (apiClient, logger) = dependency self.id = id } } 8SPOHUVQMFIFSF ❕ -
!ZPJDIJUHZ Add another dependency protocol DatabaseProtocol { ... } class
Database: DatabaseProtocol { ... } class MyViewModel { typealias Dependency = ( APIClientProtocol, LoggerProtocol, DatabaseProtocol ) let (apiClient, logger, database): Dependency let id: String init(dependency: Dependency, id: String) { (apiClient, logger, database) = dependency self.id = id } } 8SPOHUVQMFIFSF ❕ -
!ZPJDIJUHZ Add another dependency protocol DatabaseProtocol { ... } class
Database: DatabaseProtocol { ... } class MyViewModel { typealias Dependency = ( APIClientProtocol, LoggerProtocol, DatabaseProtocol ) let (apiClient, logger, database): Dependency let id: String init(dependency: Dependency, id: String) { (apiClient, logger, database) = dependency self.id = id } } -
!ZPJDIJUHZ Add another dependency container.register(MyViewModel.self) { (resolver, id: String) in
let dependency = ( resolver.resolve(APIClientProtocol.self)!, resolver.resolve(LoggerProtocol.self)! ) return MyViewModel( dependency: dependency, id: id ) } let myViewModel = container.resolve( MyViewModel.self, argument: "abc")! -
!ZPJDIJUHZ Add another dependency container.register(MyViewModel.self) { (resolver, id: String) in
let dependency = ( resolver.resolve(APIClientProtocol.self)!, resolver.resolve(LoggerProtocol.self)! ) return MyViewModel( dependency: dependency, id: id ) } let myViewModel = container.resolve( MyViewModel.self, argument: "abc")! $BOOPUDPOWFSU ❕ -
!ZPJDIJUHZ Add another dependency container.register(MyViewModel.self) { (resolver, id: String) in
let dependency = ( resolver.resolve(APIClientProtocol.self)!, resolver.resolve(LoggerProtocol.self)! ) return MyViewModel( dependency: dependency, id: id ) } let myViewModel = container.resolve( MyViewModel.self, argument: "abc")! container.register(DatabaseProtocol.self) { _ in Database() } $BOOPUDPOWFSU ❕ -
!ZPJDIJUHZ Add another dependency container.register(DatabaseProtocol.self) { _ in Database() }
$BOOPUDPOWFSU ❕ container.register(MyViewModel.self) { (resolver, id: String) in let dependency = ( resolver.resolve(APIClientProtocol.self)!, resolver.resolve(LoggerProtocol.self)!, resolver.resolve(DatabaseProtocol.self)! ) return MyViewModel( dependency: dependency, id: id ) } let myViewModel = container.resolve( MyViewModel.self, argument: "abc")! -
!ZPJDIJUHZ Add another dependency container.register(DatabaseProtocol.self) { _ in Database() }
container.register(MyViewModel.self) { (resolver, id: String) in let dependency = ( resolver.resolve(APIClientProtocol.self)!, resolver.resolve(LoggerProtocol.self)!, resolver.resolve(DatabaseProtocol.self)! ) return MyViewModel( dependency: dependency, id: id ) } let myViewModel = container.resolve( MyViewModel.self, argument: "abc")! -
!ZPJDIJUHZ "SDIJUFDUVSFJT OPUTUBUJDCVU EZOBNJDUP FWPMWF %FQFOEFODZ JOKFDUJPOJT TUJDLZMJLF HMVF 6TFBUZQFBMJBT
UPHSPVQ EFQFOEFODJFT 4VNNBSZ +VTUGPMMPX 4XJGUDPNQJMFS UPBEEOFX EFQFOEFODJFT -
!ZPJDIJUHZ More info, dziękuję! • Swinject Tutorial for iOS: Getting
Started by Gemma Barlow at raywenderlich.com • iOS App Development: Design Patterns for Mobile Architecture by Jon Bott at lynda.com • Dependency injection in Swift by Manuel Munoz at blog.quadiontech.com • Adventures in Dependency Swinjection by Ben Dietzkis at medium.com/@topLayoutGuide • Swinject - Dependency Injection in iOS by Iron Ben Zvi at speakerdeck.com/oronbz