hmlongco / Factory

A new approach to Container-Based Dependency Injection for Swift and SwiftUI.
MIT License
1.83k stars 115 forks source link

@InjectedLazyObject #149

Closed lyzkov closed 1 year ago

lyzkov commented 1 year ago

I have failed to create an @InjectedLazyObject property wrapper that could resolve dependency upon access to wrappedValue or projectedValue. Is there any hope that it is possible? Could ObservedObject<DependencyType> be created by Factory and injected with @LazyInjected?

hmlongco commented 1 year ago

Nope. Property wrappers are structs. To do lazy property wrappers you have to mutate the struct value. And you can't mutate an "observable object" property wrapper installed on a View.

I provided an InjectedObject wrapper for Factory, but at some point I'm probably going to mark it as deprecated, especially with the advent of @Observable in iOS 17.

Best to stick with @StateObject and inject needed dependencies into the view model when needed.