I am currently exploring best practices for managing Dependency Injection in iOS applications. I am particularly interested in learning how large companies handle this. Specifically, I have a few questions:
Libraries: Which Dependency Injection libraries are commonly used in the industry? (e.g) apple, meta, google) (I cannot find any information about that)
DI-Container: If team uses the DI-Container, what are some best practices to ensure its compile-time-safe and efficient use? (e.g, Needle in Uber)
I would greatly appreciate any insights or recommendations based on experiences. Thank you in advance for your help!
Personally, I recommend considering an approach where you store a heterogeneous set of values in an opaque container (much like SwiftUI.EnvironmentValues) by using something like [ObjectIdentifier: Any] (where ObjectIdentifier is made from any SwiftUI.EnvironmentKey.Type) and vending it through a TaskLocal, allowing you to "fish out your dependencies from thin air", which can be extremely ergonomic, yet very debuggable due to how TaskLocal scopes are all available in the stack trace.
Absolutely! You can have as many of them as you want and if you so choose, you can forego the TaskLocal in favor of manually passing a dependency container to the dependent component at the cost of losing the ability to transitively inject all nested dependent components.
Alternatively, you can keep track of multiple dependency containers and "hot swap" them in the TaskLocal.
Alternatively, you can spawn multiple non-nested Tasks, each of which will have their own copy of the TaskLocal.