Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default definitions with tags #20

Closed
ilyapuchka opened this issue Nov 26, 2015 · 3 comments
Closed

Default definitions with tags #20

ilyapuchka opened this issue Nov 26, 2015 · 3 comments

Comments

@ilyapuchka
Copy link
Collaborator

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
@AliSoftware
Copy link
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.

@ilyapuchka ilyapuchka removed this from the 4.1.0 milestone Nov 26, 2015
@AliSoftware
Copy link
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)

@ilyapuchka
Copy link
Collaborator Author

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
Projects
None yet
Development

No branches or pull requests

2 participants