tschneidereit / SwiftSuspenders

NOTE: Find the offical repo at http://github.com/robotlegs/swiftsuspenders
https://github.com/robotlegs/swiftsuspenders
MIT License
280 stars 89 forks source link

DependencyProvider and getInstance() #61

Closed darscan closed 12 years ago

darscan commented 12 years ago

What is provided as the targetType to apply() when getInstance() is called manually? I have a situation where I need to use getInstance() rather than an Inject point, but the provider needs the targetType. At the moment it seems to pass the requested type.

tschneidereit commented 12 years ago

Yep, it's the requested type. I only saw the option of passing null or the requested type itself, both not making too much sense, really.

The only way to pass the targetType for getInstance would be to change that method's signature. Unfortunately, I think that that's pretty hard to understand. Implementing your own dependency provider really is something entirely different from using the injector's default API, don't you think?

On Thu, Nov 10, 2011 at 20:32, Shaun Smith reply@reply.github.com wrote:

What is provided as the targetType to apply() when getInstance() is called manually? I have a situation where I need to use getInstance() rather than an Inject point, but the provider needs the targetType. At the moment it seems to pass the requested type.


Reply to this email directly or view it on GitHub: https://github.com/tschneidereit/SwiftSuspenders/issues/61

darscan commented 12 years ago

Yeh, it's a tricky one. It cropped up while creating a provider for loggers:

https://github.com/robotlegs/robotlegs-framework/blob/4149bed1f83ef04e09d18e21c4f8ccc1420f6691/src/org/robotlegs/v2/extensions/logging/integration/LoggerProvider.as

There are some places where I need to pull out a fresh logger with getInstance() as the class I'm in doesn't get injected into. The only solutions I can think of are:

Expanding the getInstance signature:

getInstance(type, name="", targetType="")

or adding a new method:

getInstanceFor(type, name="", targetType="")

neither of which are terribly pretty :/