-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamically resolve differently for same protocl #96
Comments
Indeed this functionality is currently not quite supported by SwinjectStoryboard - I have answered more in depth in the SO issue. |
From @gilgol on April 10, 2018 11:33 First of all thanks for the quick response. Currently when calling What if you would specify in the storyboard the available |
Thanks for the idea 👍 If we wanted to add the However, I'm not quite sure whether this is the right path to take: Initial idea behind the SwinjectStoryboard was to allow users relying on "hidden" instantiation of view controllers from storyboard (e.g. segues, child containers, ..) to inject dependencies. Once you need to use names in instantiation you cannot rely on these mechanisms anyway, so you might as well instantiate the view controllers manually: container.register(MyViewController.self, name: "name") {
let vc = SwinjectStoryboard.create(name: "MyStoryboard", bundle: nil).instantiateViewController(withIdentifier: "identifier")
vc.viewModel = $0.resolve(MyViewModel.self)!
return viewModel
}
let vc = SwinjectStoryboard.defaultContainer.resolve(MyViewController.self, name: "name") My fear is that once we start extending BTW: this argument works for adding resolution arguments (SwinjectStoryboard #62), which is taking the framework in the same direction. |
From @gilgol on April 10, 2018 14:54 Well, according to your answer and a brief look on the implementation in Will adding a default value Because as I see it - from the moment you've instantiated the view controller from the storyboard, all you do is injecting the dependancy and return the injected viewController.
could cause any issues and will move the library to the direction you mentioned is wrong? |
@gilgol We are on the same page, the example implementation you've came up with is more or less what I had in mind when thinking about this feature 😄 I'm not so much worried about the issues with the implementation itself - IMO it would work just fine - but with extending the SwinjectStoryboard API in general. Unfortunately, I haven't considered this when we were discussing the API for arguments.. Any extension will introduce more complexity to the codebase, and we should weigh that against the benefits it brings. From my point of view this kind of usage diverges from the original purpose of SwinjectStoryboard, and can be implemented with just the Swinject API. On the other hand I can see the argument for maintaining consistency with the cases where you need to use the SwinjectStoryboard. However, I don't have much real world experience with SwinjectStoryboard - I am not using it in my bigger projects. Thoughts, anyone? |
From @gilgol on April 9, 2018 11:40
I'm trying to dynamically resolve a protocol
ViewModeling
that 2 different view models are conforming to..I am able to do it on the service level (one protocol that 2 services are conforming to, and 2 different viewModel that each consume a different service can resolve the required one using a specific
name
)I'm struggling with doing this on the viewController level, trying to inject the same view controller a specific viewModel (both conforms as mentioned to the same protocol).
I believe the issue is with
SwinjectStoryboard
that doesn't let me instantiate a view controller using its storyboard id (like I would normally do), and in addition define few different names that will be resolved on runtime.Am I missing something?
Just realised it might be more appropriate as a stackoverflaw post, so here is a link to the post:
https://stackoverflow.com/questions/49732300/how-to-inject-the-right-viewmodel-to-a-single-viewcontroller
Copied from original issue: Swinject/Swinject#345
The text was updated successfully, but these errors were encountered: