-
Notifications
You must be signed in to change notification settings - Fork 89
Cannot instantiate an Interface - even if there is a Rule for it #15
Comments
Ooh, I like that idea! Will definitely integrate it into the next release. I guess the behavior should be as follows: Thoughts on these results? |
I agree with: Input: Interface for which the injector doesn't have any rule mapped | Output: throw InjectionError "no class mapping found while trying to instantiate interface X" But I think if any rule exists, it should return the appropriate instance. I.E:
should return the singleton Car instance - regardless of whether or not that instance already exists. It should be used to retrieve a dependency of a given type. Or, put another way: instantiate() might have been better named as getOrCreateInstance(). What do you think? (obviously, I'm not suggesting changing the name, just using that to illustrate the point). |
Mmh. While I see the utility of being able to get the result of any mapping, I really think that that functionality shouldn't be provided by a function named "instantiate". What about |
I totally agree that the name doesn't fit with the functionality. Though I'm not sure that the proposed names are better. I think it's over complicating things to refer to the mappings (an implementation detail). If we go back to the beginning, and think about what an IoC container is, then the desired functionality is simply about getting an instance from the container - without knowing anything about the rules that might create/find such an instance. It shouldn't matter whether the rules generate a new instance or re-use an old one - we shouldn't care/know about that from the outside. We just want an instance. |
Yup, that's how see it as well. But it seems to me that this means we really can't use How about adding |
I think that's a brilliant plan! |
Cool - I'll implement it in the next few days and roll it into the 1.5 release. |
Awesome =) No rush from my side - was just trying to put together a little DI tutorial, and this popped up. |
getInstance is in 1.5.0b4 |
And now to actually close this issue ... |
I missed this when I originally asked about it on the forum. Great insight guys also for deferring deprecating #instantiate :) |
I kinda suspected that this wouldn't work:
but don't you think it should?
The text was updated successfully, but these errors were encountered: