@propertyWrapper based dependency injection without singleton

Hello, I see that people are using @propertyWrappers for dependecy injection, for example in those articles:

https://medium.com/swift2go/dependency-injection-with-property-wrappers-1a8a07a4124c
https://medium.com/streamotion-tech-blog/magic-dependency-injection-in-swift-70476c7743ec

But anyway they are using singleton at some point. And for example there is workaround for unit-tests in second article, to inject correct instances into singleton by using queue.
There is any way to use DI based on @propertyWrapper without singletons?

You can access the enclosing self instance inside of a property wrapper (see Accessing a Swift property wrapper’s enclosing instance | Swift by Sundell) - this may help you write an API without a singleton, you would be required to pass along some context throughout your application which may not be advantageous in some app, a singleton is usually used because it's easier and more convenient.

I do think there is room for what you are suggesting and it would be worth exploring in more detail

1 Like