A new approach to Container-Based Dependency Injection for Swift and SwiftUI. Factory is strongly influenced by SwiftUI, and in my opinion is highly suited for use in that environment. Most container-based dependency injection systems require you to define in some way that a given service type is available for injection and many require some sort of factory or mechanism that will provide a new instance of the service when needed. Unlike Resolver which often requires defining a plethora of nested registration functions, or SwiftUI, where defining a new environment variable requires creating a new EnvironmentKey and adding additional getters and setters, here we simply add a new Factory computed variable to the default container. When it's called our Factory is created, its closure is evaluated, and we get an instance of our dependency when we need it.
Features
- Factory doesn't tie you down to a single dependency injection strategy or technique
- Factory supports containers, scopes, passed parameters, contexts, decorators, unit tests, SwiftUI Previews, and much, much more
- Little to no setup time is needed for the vast majority of your services, resolutions are extremely fast, and no compile-time scripts or build phases are needed
- Factory is compile-time safe; a factory for a given type must exist or the code simply will not compile
- Defining a registration usually takes just a single line of code. Same for resolution
- Factory is free and open source under the MIT License