tschneidereit/SwiftSuspenders — pull request #78 by Stray (2012-10-31 12:27:53+00:00)
Title: instantiateUnmapped dependency tree use cases

Hi Till,

as I see it there are 2 usecases:

1) "Instantiate a Thing, using the injector mappings for the dependency tree"

2) "Instantiate a Thing, using fresh instances for the dependency tree"

Any hybrid of these would need to be implemented by the user using a custom dependency provider.

So, the method is now:

`instantiateUnmapped(type : Class, useMappingsForDependencyTree : Boolean = true)`

with uses:

1) `instantiateUnmapped(Thing);`

2) `instantiateUnmapped(Thing, false);`

In case (1), if you have unmapped dependencies in the tree, you need to provide a suitable fallbackProvider and it will choke if this can't provide for everything you need.

In case (2), the injector will assume that you want to use the FreshInstanceProvider for all dependencies, even if they have been mapped previously.

It was fairly straightforward to get the tests passing. I've created the FreshInstanceProvider and in case (2) it simply generates a new injector that has no mappings and then passes off the work to that. You might want to cache that injector - I've just done the simplest thing that can work.

The FreshInstanceProvider is saving the type passed to the `prepareNextRequest(...)` method and using `injector.instantiateUnmapped(...)` to fulfil this. If `prepareNextRequest` and `apply` were ever non-sequential this could be a problem - but I can't see how they could be.

Ta!

Stray