Skip to content

Default definitions with tags #20

Closed

Description

@ilyapuchka

Currently we can register few definitions with different tags and one with no tag, which will be used as a default if we try to resolve component using unknown tag.

But it does not work the other way - if we don't register definition without tag we will not be able to resolve component without providing tag.

Would it be useful to be able to setup definition with tag as a default and later be able to resolve it without setting tag?

container.register(tag: "facebook", useByDefault: true) { FacebookOAuth() as OAuth }
container.register(tag: "twitter") { TwitterOAuth() as OAuth }
container.register(tag: "google") { GoogleOAuth() as OAuth }

container.resolve() as OAuth // will resolve FacebookOAuth

container.register() { TwitterOAuth() as OAuth } // will override previous default
container.resolve("tumblr") as OAuth // should not resolve, because user explicitly specified unknown tag

Activity

  1. added this to the 4.1.0 milestone on Nov 26, 2015
  2. AliSoftware commented on Nov 26, 2015

    @AliSoftware
    Owner

    Interesting idea but I don't think it's worth it. one can simply register the same block with both the tag and nil if they want that behavior.
    I think that kind of logic belong to each application's code anyway and is not worth adding a parameter (even optional) for that marginal case.

  3. removed this from the 4.1.0 milestone on Nov 26, 2015
  4. AliSoftware commented on Nov 26, 2015

    @AliSoftware
    Owner

    One way people could do it in their project on their own:

    let defaultFactory = { FacebookOAuth() as OAuth }
    container.register(factory: defaultFactory) 
    container.register(tag: "Facebook", factory: defaultFactory)
    container.register(tag: "twitter") { TwitterOAuth() as OAuth }
    container.register(tag: "google") { GoogleOAuth() as OAuth }
    

    Alternate way:

    container.register(tag: "Facebook") { FacebookOAuth() as OAuth }
    container.register(tag: "twitter") { TwitterOAuth() as OAuth }
    container.register(tag: "google") { GoogleOAuth() as OAuth }
    container.register() { [unowned container] in container.resolve("Facebook") as OAuth }
    

    (Both are untested code, just ideas typed live in GitHub)

  5. ilyapuchka commented on Dec 6, 2015

    @ilyapuchka
    CollaboratorAuthor

    Yes, probably that could add additional confusion about what definition will be used without any big win.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions