Skip to content
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

Injected property wrapper for better usage #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gf0x
Copy link

@gf0x gf0x commented Jan 17, 2020

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 in UIViewController, we usually want it to be private to provide some kind of encapsulation.

class MyViewController: UIViewController {

private var viewModel: MyViewModel!

...
}

However, it's not possible to do that if we resolve it outside the UIViewController which is especially true if we use SwinjectStoryboard and resolve this value in the storyboardInitCompleted.

So we usually fall back to this:

var viewModel: MyViewModel!

With the use of my approach we can easily write:

@Injected private var viewModel: MyViewModel!

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.

@Injected(SwinjectStoryboard.defaultContainer, "myDependency") private var viewModel: MyViewModel!

I feel really bad about having to write so many inits instead of one but it's the only way to deal with this swift bug

I'm looking forward to see this in future releases!

@kleber11
Copy link

Thanks man! Finally I've got better solution for such problem!

@tkohout tkohout removed their request for review May 12, 2023 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants