Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
#Swift3Arch
Dependence day:
Insurgence
Jorge D. Ortiz Fuentes
@jdortiz
A Canonical

Examples

Production
#Swift3Arch
#Swift3Arch
Agenda
The Problem
Solutions
Recommendations
The Problem
#Swift3Arch
Job Description
Understand the problem
Identify the right set of pieces
Put proper names
Make them run togethe...
#Swift3Arch
Social Network Option A
Social Network
#Swift3Arch
Video
Text
Image
Social Network Option B
User PostGroup
Discussion
Promotion
DM
#Swift3Arch
Single Responsibility
Principle
View
Controller

Presenter

View Model
Model
Presentation
logic
Business
Logic...
The Jigsaw
Puzzle
#Swift3Arch
Making it work
Objects require other objects to work ->
Dependencies
Each object creates its dependencies
Bad ...
Instanciation
=

alien is born
Test
environment
#Swift3Arch
How to Provide
Dependencies
Initializer
Property
Method
But Who?
Frameworks to the
rescue
#Swift3Arch
Inversion of Control
Hollywood Principle
#Swift3Arch
Dependency Inversion
Containers
Dip
Swinject
Cleanse
#Swift3Arch
Dip
Created by Olivier Halligon, maintained by Ilya
Puchka
Beautifully simple
Nice way to inject in Storyboard...
#Swift3Arch
Dip
func configure(container: DependencyContainer) {
container.register(.unique) { InMemoryRepo() }
container....
#Swift3Arch
Swinject
Created by Jakub Vano, Wolfgang Lutz, Yoichi
Tagala
Pure Swift Type Support, Injection with
Arguments...
#Swift3Arch
Cleanse
Created by Square
Early version, but promising
No Swift 3
Yet*!
The World Outside
#Swift3Arch
Java for Android
Standard annotations for DI (JSR330)
Introspection & Reflection
Many frameworks available
Som...
Maturity
What then?
Traditional
methods
#Swift3Arch
Assemble the Pieces
class ProgrammersListConnector {
// MARK: - Constants
let entityGateway: EntityGatewayProt...
#Swift3Arch
Factories are your friend
protocol UseCase { func execute() }
class UseCaseFactory {
enum Use {
case showProgr...
Recap
#Swift3Arch
Take aways
Make your dependencies explicit
Use factories
Understand how it a container works
Use DI frameworks...
Thank
You!
@jdortiz
#Swift3Arch
Upcoming SlideShare
Loading in …5
×

Dependence day insurgence

383 views

Published on

The state of the art about dependency injection with Swift for iOS and my personal advice on how to approach the problem.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Dependence day insurgence

  1. 1. #Swift3Arch Dependence day: Insurgence Jorge D. Ortiz Fuentes @jdortiz
  2. 2. A Canonical
 Examples
 Production #Swift3Arch
  3. 3. #Swift3Arch Agenda The Problem Solutions Recommendations
  4. 4. The Problem
  5. 5. #Swift3Arch Job Description Understand the problem Identify the right set of pieces Put proper names Make them run together properly
  6. 6. #Swift3Arch Social Network Option A Social Network
  7. 7. #Swift3Arch Video Text Image Social Network Option B User PostGroup Discussion Promotion DM
  8. 8. #Swift3Arch Single Responsibility Principle View Controller
 Presenter
 View Model Model Presentation logic Business Logic Data Source Data Source Data Source Navigation
  9. 9. The Jigsaw Puzzle
  10. 10. #Swift3Arch Making it work Objects require other objects to work -> Dependencies Each object creates its dependencies Bad idea™ • Another responsibility • What if we want to use it with different sets of pieces?
  11. 11. Instanciation =
 alien is born
  12. 12. Test environment
  13. 13. #Swift3Arch How to Provide Dependencies Initializer Property Method
  14. 14. But Who?
  15. 15. Frameworks to the rescue
  16. 16. #Swift3Arch Inversion of Control Hollywood Principle
  17. 17. #Swift3Arch Dependency Inversion Containers Dip Swinject Cleanse
  18. 18. #Swift3Arch Dip Created by Olivier Halligon, maintained by Ilya Puchka Beautifully simple Nice way to inject in Storyboards Scopes, Named definitions, Runtime arguments, Circular dependencies, Auto-wiring & Auto-injection, Type forwarding, Storyboards integration, Weakly typed components, Easy configuration, Thread safety, Helpful error messages, and configuration validation.
  19. 19. #Swift3Arch Dip func configure(container: DependencyContainer) { container.register(.unique) { InMemoryRepo() } container.register(.unique) { try UseCaseFactory(entityGateway: container.resolve() as InMemoryRepo) } DependencyContainer.uiContainers = [showProgrammersListModule] } let showProgrammersListModule = DependencyContainer { container in container.register() { try ProgrammersListPresenter(useCaseFactory: container.resolve() as UseCaseFactory) } container.register(tag: "ProgrammersTableVC") { ProgrammersTableViewController() } .resolvingProperties { container, viewController in viewController.presenter = try container.resolve() as ProgrammersListPresenter } } extension ProgrammersTableViewController: StoryboardInstantiatable {}
  20. 20. #Swift3Arch Swinject Created by Jakub Vano, Wolfgang Lutz, Yoichi Tagala Pure Swift Type Support, Injection with Arguments, Initializer/Property/Method Injections, Initialization Callback, Circular Dependencies, Object Scopes, Reference & Value Types, Self-registration, Container Hierarchy, Property Injection from Resource files, Thread Safety, Modular Components, Storyboard
  21. 21. #Swift3Arch Cleanse Created by Square Early version, but promising
  22. 22. No Swift 3 Yet*!
  23. 23. The World Outside
  24. 24. #Swift3Arch Java for Android Standard annotations for DI (JSR330) Introspection & Reflection Many frameworks available Some owned by Google (Guice & Dagger 2)
  25. 25. Maturity
  26. 26. What then?
  27. 27. Traditional methods
  28. 28. #Swift3Arch Assemble the Pieces class ProgrammersListConnector { // MARK: - Constants let entityGateway: EntityGatewayProtocol weak var presenter: ProgrammersListPresenter! func assembleModule(view: ProgrammersTableViewController) { let useCaseFactory = UseCaseFactory(entityGateway: entityGateway) let presenter = ProgrammersListPresenter(useCaseFactory: useCaseFactory) view.presenter = presenter view.connector = self presenter.view = view self.presenter = presenter } }
  29. 29. #Swift3Arch Factories are your friend protocol UseCase { func execute() } class UseCaseFactory { enum Use { case showProgrammersList(completion: ShowProgrammersListCompletion) // … } func create(useCase whichUseCase: Use) -> UseCase { switch whichUseCase { case let .showProgrammersList(completion: completion): return ShowProgrammersListUseCase(entityGateway: entityGateway, completionHandler: completion) // … } }
  30. 30. Recap
  31. 31. #Swift3Arch Take aways Make your dependencies explicit Use factories Understand how it a container works Use DI frameworks with caution
  32. 32. Thank You!
  33. 33. @jdortiz #Swift3Arch

×