square / Cleanse

Lightweight Swift Dependency Injection Framework
Other
1.78k stars 90 forks source link

Advantage of using Tags #29

Closed thebarndog closed 8 years ago

thebarndog commented 8 years ago

This framework is fantastic but the one thing I can't wrap my head around is

In the example:

extension UIWindow {
  struct Module : Cleanse.Module {
    public func configure<B : Binder>(binder binder: B) {
      binder
        .bind(UIWindow.self)
        .asSingleton()
        .to { (rootViewController: TaggedProvider<UIViewController.Root>) in
          let window = UIWindow(frame: UIScreen.mainScreen().bounds)
          window.rootViewController = rootViewController.get()
          return window
        }
    }
  }
}

and

extension RootViewController {
  /// Configures RootViewController
  struct Module : Cleanse.Module {
    func configure<B : Binder>(binder binder: B) {
      // Configures the RootViewController to be provided by the initializer
      binder
        .bind()
        .to(factory: RootViewController.init)

      // This satisfies UIWindow depending on TaggedProvider<UIViewController.Root>
      // The actual root is our RootViewController wrapped in a UINavigationController
      binder
        .bind()
        .tagged(with: UIViewController.Root.self)
        .to { UINavigationController(rootViewController: $0 as RootViewController) }
    }
  }
}

are equivalent to:

What would the advantage of using tags be in this situation? Is it solely for API clarity?

Continuing with the api url example used in the readme, I constructed some possible use cases for tags (as I understand them):

extension NSURL {
    struct Primary: Tag {
        typealias Element = NSURL
    }
    struct Github: Tag {
        typealias Element = NSURL
    }
}

struct APIModule: Cleanse.Module {

    func configure<B: Binder>(binder binder: B) {
        binder
            .bind()
            .to(factory: APIModule.init)
        binder
            .bind()
            .tagged(with: NSURL.Primary.self)
             .to { NSURL(string: "https://myapi.com")!  }
}

struct GithubModule: Cleanse.Module {

    func configure<B: Binder>(binder binder: B) {
        binder
            .bind()
            .to(factory: GithubModule.init)
        binder
            .bind()
            .tagged(with: NSURL.Github.self)
            .to { NSURL(string: "https://github.com")! }
    }
}

Is that a correct usage of tags? It seems a bit contrived, distinguishing between plain NSURL instances but I can see there being a value in tagging more complex classes.

holmes commented 8 years ago

You've got it exactly right. It's mostly for API clarity and differentiating between Element types. Examples always feel a bit contrived, but yours captures it just fine. An API, Github link and a support link could all be NSURL and you would have to differentiate between them.

thebarndog commented 8 years ago

That makes perfect sense, thanks!