Skip to content

Instantly share code, notes, and snippets.

@53ningen
Last active March 8, 2016 16:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 53ningen/93660070dedb25377032 to your computer and use it in GitHub Desktop.
Save 53ningen/93660070dedb25377032 to your computer and use it in GitHub Desktop.

Dependency Injection in Swift 2

今日話す内容

中〜大規模 Swift 開発における依存オブジェクト解決の方法を考える

  1. DI (Dependency Injection) とは?
  • DIの基本的な話:知っている人が多かったら省略予定
  1. コンテナ (Swinject) を用いた DI
  • Swinject の DI コンテナを用いた 動的DIの話
  1. Swiftにおける静的 DI
  • Cake-Pattern風の静的DIをSwiftでなんとか実現する話

Dependency Injection とは?

依存オブジェクトをなんらかの方法を使って外部から差し込む形にすることをDependency Injectionという

  • コンストラクタからの注入(コンストラクタ・インジェクション)
  • セッターからの注入(セッター・インジェクション)
  • フィールドへの注入(フィールド・インジェクション)
  • ミックスインによる注入(ミックスイン・インジェクション)

など...

やっていることは依存オブジェクトを外部から差し込んでるだけ

配線の問題

複雑なシステムではたくさんの依存オブジェクトを解決する必要がある

実際に起こりうるケース

  • LoginViewControllerは、UserServiceに依存する
  • UserServiceの実装は、UserRepositoryに依存する
  • UserRepositoryの実装は、AppApiClientに依存する
  • AppApiClientの実装は、HttpClientに依存する

モジュール化とDIにより責務分離ができたが、クライアントコードからサービスのインスタンスを取得するのが難しくなってしまった 😇

コンテナ(Swinject)を用いたDI

Swiftにおける静的DI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment