Injected property wrapper for better usage #139
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I'd like to suggest a feature that I was lacking: property wrapper for the DI. Finally thanks to Swift 5 updates it was possible to implement.
Let me explain the problem it solves in a few words.
If we have
viewModel
injected inUIViewController
, we usually want it to be private to provide some kind of encapsulation.However, it's not possible to do that if we resolve it outside the
UIViewController
which is especially true if we useSwinjectStoryboard
and resolve this value in thestoryboardInitCompleted
.So we usually fall back to this:
With the use of my approach we can easily write:
This gives us the ability to preserve encapsulation, inject property without problems and keep our code clear and nice.
Moreover, we can specify the container for injection to be resolved from and/or name of the dependency.
I feel really bad about having to write so many
init
s instead of one but it's the only way to deal with this swift bugI'm looking forward to see this in future releases!