An ultralight Dependency Injection / Service Locator framework for Swift 4 and iOS.
Dependency Injection frameworks support the Inversion of Control design pattern. Technical definitions aside, dependency injection pretty much boils down to:
| Giving an object the things it needs to do its job.
That's it. Dependency injection allows us to write code that's loosely coupled, and as such, easier to reuse, to mock, and to test.
For more, see: A Gentle Introduction to Dependency Injection.
There are five classic dependency injection strategies:
Resolver supports them all. Follow the links for a brief description, examples, and the pros and cons of each.
Resolver is implemented in just over 300 lines of actual code, but it packs a ton of features into those 300 lines.
- Automatic Type Inference
- Scopes: Application, Cached, Graph, Shared, and Unique
- Protocols
- Optionals
- Named Instances
- Argument Passing
- Custom Containers & Nested Containers
- Storyboard Support
TLDR: If nothing else, make sure you read about Automatic Type Inference, Scopes, and Optionals.
Using Resolver is a simple, three-step process:
- Add Resolver to your project.
- Register the classes and services your app requires.
- Use Resolver to resolve those instances when needed.
As mentioned, Resolver is an ultralight Dependency Injection system, implemented in just over 300 lines of code and contained in a single file.
Resolver is also designed for performance. SwinjectStoryboard, for example, is a great dependency injection system, but Resolver clocks out to be about 800% faster at resolving dependency chains than Swinject.
And unlike some other systems, Resolver is written in 100% Swift 4, with no Objective-C code, method swizzling, or internal dependencies on the Objective-C runtime.
Further, Resolver:
- Is tested in production code.
- Is thread safe (assuming your objects are thread safe).
- Has a complete set of unit tests.
- Is well-documented.
Finally, with Automatic Type Inference you also tend to write about 40-60% less dependency injection code using Resolver.
Resolver was designed, implemented, and documented by Michael Long, a Senior Lead iOS engineer at CRi Solutions. CRi is a leader in developing cutting edge iOS, Android, and mobile web applications and solutions for our corporate and financial clients.
- Email: [email protected]
- Twitter: @hmlco
Resolver is available under the MIT license. See the LICENSE file for more info.