Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dependency Injection in iOS Apps

Dependency Injection in iOS Apps

yashigani

April 26, 2016
Tweet

More Decks by yashigani

Other Decks in Programming

Transcript

  1. %FQFOEFODZ*OKFDUJPO

    JOJ04"QQT
    @yashigani
    ؔϞό #13

    View Slide

  2. yashigani
    @yashigani
    http://yashigani.hatenablog.com

    View Slide

  3. 8IBUJT
    %FQFOEFODZ*OKFDUJPO

    View Slide

  4. • Depend interface, not depend implementations
    • Make less dependency between components
    • Improve testability
    • Make easy to mock dependency objects

    View Slide

  5. )PXUPJOKFDU
    EFQFOEFODZPCKFDUT

    View Slide

  6. // Constructor injection
    class ViewController: UIViewController {
    let client: APIClient
    init(client: APIClient) {
    self.client = APIClient
    }
    }

    View Slide

  7. • Pros
    • Easy to control
    • Cons
    • Don’t fit storyboard
    • Need dependency objects to create

    View Slide

  8. // Property injection
    override func prepareForSegue() {
    if let vc =
    segue.dVC as? NextViewController
    {
    vc.client = client
    }
    }

    View Slide

  9. • Pros
    • Don’t need to dependency objects to create
    • Fit view controller with storyboard
    • Cons
    • Too difficult to stay simple
    • Overtime pass to dependency objects

    View Slide

  10. // Hold all dependency objects in app
    class Context {
    // Singleton!!!
    private static let context = Context()
    static func get() -> Context {
    return context
    }
    let client = APIClient()
    }
    class ViewController: UIViewController {
    // Depend to singleton pattern
    let context = Context.get()
    }

    View Slide

  11. View Slide

  12. 6TJOH
    %FQFOEFODZJOKFDUJPO

    View Slide

  13. View Controller
    Context

    View Slide

  14. View Controller
    ViewController:
    WithContext
    WithContext
    Context
    ContextType

    View Slide

  15. // protocol for Context
    protocol ContextType {
    var client: APIClientType { get }
    }
    class Context: ContextType {
    private static let context = Context()
    static func get() -> Context {
    return context
    }
    let client: APIClientType
    = APIClient()
    }

    View Slide

  16. protocol WithContext {
    var context: ContextType { get }
    }
    extension WithContext {
    var context: ContextType {
    return Context.get()
    }
    }
    class ViewController:
    UIViewController, WithContext {}

    View Slide

  17. • Default behavior, WithContext return static Context
    object
    • In unit test, add extension ViewController.context
    • Change context object to mock
    • Easy to mock context because it depend to
    ContextType
    • Solve strong dependency to singleton pattern

    View Slide

  18. 3FDBQ

    View Slide

  19. • Don’t depend to implementations, depend to
    protocols
    • DI makes easy and flexible to inject dependency
    objects
    • Depending protocols makes easy to change
    implementations
    • Fit View Controller with storyboard very much ✨

    View Slide

  20. 5IBOLZPV

    View Slide