Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

getInstance implies that I will receive an instance irrespective of mapping. getMappedInstance would be more appropriate API #25

Closed
joelhooks opened this issue Apr 24, 2010 · 9 comments

Comments

@joelhooks
Copy link
Contributor

I still really think that getInstance should not throw an error, but instead provide an instance of the class via Injector#instantiate. The API suggests to me that I am going to get an instance, not an error. Otherwise it should be getMappedInstance to let me know that is a very specific call.

@joelhooks
Copy link
Contributor Author

I went ahead and made this change in my childinjectors branch on my fork.

@tschneidereit
Copy link
Owner

I see that you've since rolled back your change. What made you reconsider?

@joelhooks
Copy link
Contributor Author

shaun yelled at me. ;) - not really, he sent me links and didn't like the change. I still don't like it, but it is consistant with SmartyPants and others so it is cool. I know it was debated a lot.

@tschneidereit
Copy link
Owner

hehe ;)
Yeah, that API wasn't an easy design at all ...

@darscan
Copy link
Contributor

darscan commented May 20, 2010

"shaun yelled at me" hahaha!! that prick! but seriously, if getInstance didn't throw an error the world would become a strange and unpredictable place. The word "or" is the clue here. "Retrieve an instance from the container if a rule exists OR instantiate a new instance if one does not". There would be no way to tell the two scenarios apart.

This is especially dangerous with singletons - you wouldn't be able to tell if you are being given a singleton (as you might expect due to some design) or a new instance. "instantiate" already causes some confusion in this regard: people map a singleton and then use "instantiate" expecting to be given the same instance that other parts of the system will be given.. but that's not what happens of course. "getInstance" is the remedy to this scenario, and having it fall back to "instantiate" behavior just muddies the waters :)

@tschneidereit
Copy link
Owner

I fully concur!

I also agree about instantiate being a bit unfortunate. I think 2.0 will see some interface cleanup during which instantiate might just as well get lost.

@darscan
Copy link
Contributor

darscan commented May 20, 2010

Ah yes, but instantiate is still useful: MediatorMap, CommandMap - places where we actually want new instances (regardless of rules) with fully satisfied dependencies (including ctor args).

@tschneidereit
Copy link
Owner

You absolutely insist on having the last word on this entire topic, don't you? ;)

(Obviously, being right helps with that ...)

@darscan
Copy link
Contributor

darscan commented Jun 10, 2010

Yes.

This issue was closed.
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants