Swinject / SwinjectStoryboard

Swinject extension for automatic dependency injection via Storyboard
MIT License
268 stars 141 forks source link

Storyboard injection with `Assembly` #65

Closed atomicbird closed 6 years ago

atomicbird commented 7 years ago

I'm working on an app that uses Swinject via the Assembly approach to managing injection. I'm looking at adding support for storyboard injection.

Since we're currently instantiating the window and main view controller from the storyboard, I think I need to use SwinjectStoryboard.setup() to manage storyboard injection. But the defaultContainer used there isn't the same as the container argument passed to my Assembly subclass. Also, setup() is called early enough that I don't think I can create my assembly objects before setup() is called.

Is there some way I can use SwinjectStoryboard to manage a dependency on an object that's created in an Assembly? It's possible I haven't grasped Swinject well enough to see the obvious approach here.

mpdifran commented 7 years ago

When you create your assembly, pass the Storyboard's container into the constructor like so:

let assembler = Assembler(container: SwinjectStoryboard.defaultContainer)
yoichitgy commented 7 years ago

Thanks @mpdifran for the answer👍