Swiftpack.co - Package -

Resolver icon

An ultralight Dependency Injection / Service Locator framework for Swift 5.1 on iOS.

Introduction

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.

Dependency Injection Strategies

There are six classic dependency injection strategies:

  1. Interface Injection
  2. Property Injection
  3. Constructor Injection
  4. Method Injection
  5. Service Locator
  6. Annotation (NEW)

Resolver supports them all. Follow the links for a brief description, examples, and the pros and cons of each.

Property Wrappers

Resolver now supports resolving services using the new property wrapper syntax in Swift 5.1.

class BasicInjectedViewController: UIViewController {
    @Injected var service: XYZService
    @LazyInjected var service2: XYZLazyService
}

Just add the Injected keyword and your dependencies will be resolved automatically. See the Annotation documentation for more on this and other strategies.

Features

Resolver is implemented in just over 300 lines of actual code, but it packs a ton of features into those 300 lines.

TLDR: If nothing else, make sure you read about Automatic Type Inference, Scopes, and Optionals.

Using Resolver

Using Resolver is a simple, three-step process:

  1. Add Resolver to your project.
  2. Register the classes and services your app requires.
  3. Use Resolver to resolve those instances when needed.

Installation

Resolver supports CocoaPods and the Swift Package Manager.

pod " Resolver"

Resolver itself is just a single source file (Resolver.swift), so it's also easy to simply download the file and add it to your project.

Note that the current version of Resolver (1.1) supports Swift 5.1. For Swift 5 or Swift 4, checkout an earlier version.

Why Resolver?

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 5, 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.

Author

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.

License

Resolver is available under the MIT license. See the LICENSE file for more info.

Additional Resouces

Github

link
Stars:
Help us keep the lights on

Dependencies

Used By

Total: 0

Releases

Update project for Swift 5.1 and SPM - 2019-12-02 20:07:14

  • Update project for Swift 5.1 and SPM
  • Add @Injected property wrapper for Swift 5.1
  • Add @LazyInjected property wrapper for Swift 5.1
  • Revise unit tests for more code coverage
  • Make static registration function public and add concurrency mutexes
  • Add type specification to defaultScope
  • Fix initializers on scope types to allow public instantiation.
  • Allow clearing the shared cache

Update project for Swift 4.2 - 2019-02-18 19:27:17

  • Update project for Swift 4.2
  • Ensure all tests pass under new environment

Note project file changes only. No code changes over 1.0.6.

Fix bug resolving shared protocols - 2019-02-18 19:08:46

  • Fix bug resolving shared protocols
  • Add passed resolver option to factories

Minor improvements - 2018-06-09 15:31:54

  • Changed registrationsNeeded to performInitialRegistrations closure.
  • Value types not cached during graph resolution.

Code size optimizations - 2018-05-06 17:38:15

Fix issue with caching named instances - 2018-05-06 00:49:35

Fix issue with caching named instances.

Debug support for optionals - 2018-04-06 13:16:44

Make ResolverScopeCache intializer visible - 2018-04-03 13:14:37

Make ResolverScopeCache intializer visible

Full release with documentation - 2018-04-01 12:43:40

Cocoapods Release - 2018-03-29 19:32:56

Initial release for Cocoapods.