Swiftpack.co - miquido/MQDo as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by miquido.
miquido/MQDo 0.9.2
Swift features management and dependency injection framework. The project was made by Miquido: https://www.miquido.com/
โญ๏ธ 4
๐Ÿ•“ 1 week ago
iOS macOS watchOS tvOS macCatalyst
.package(url: "https://github.com/miquido/MQDo.git", from: "0.9.2")


Platforms Swift Package Manager compatible SwiftVersion

Dependency injection framework for Swift.


MQDo provides numerous functionalities useful for managing dependencies across Swift codebases:

  • dependency container tree with support for branches and scopes
  • scopes with automatic access and lifetime management
  • multiple dependency trees (multiple, independent tree roots)
  • multiple implementations of the same type within a single tree based on scopes
  • multiple instances of the same type within a single tree based on scopes
  • dynamic implementation selection
  • nested dependencies
  • cache and lazy loading
  • effortless mocking and testing
  • error diagnostics


Basic example of a container, feature definition and feature access.

Firstly you have to prepare feature interface. All interfaces should be defined using structs. It helps in both testing (mocking) and debugging. Using structs instead of protocols is also required due to swift compiler limitations in some cases.

// Interface of a feature prepared using a struct.
struct Printer {

  // Method required by the interface
  var printMessage: (String) -> Void

Then define its conformance to a required protocol allowing usage in dependency containers. Depending on selected protocol it also defines a lifetime and accessibility of a feature. StaticFeature is always available and have to be defined. DisposableFeature is built each time instance is requested returning fresh instance each time. CacheableFeature is built once for defined scope, cached and reused when able.

// Conformance to one of a "Feature" protocols is required. 
// Dynamic features (Disposable, Cacheable) can have a Context defined or be contextless (which is default).
// Context is then required to create instances and can be used to pass any additional data and methods inside.
extension Printer: DisposableFeature {

  // It is required to provide placeholder implementation.
  // Placeholders are then used as a base for mocking.
  static var placeholder: Self {
      printMessage: unimplemented1()

Next you can provide any number of implementations. You can do it ad-hoc (as in example below) or by using only a concrete type which will be used to fulfill interface requirements.

// Implementation of a feature can be provided by defining
// FeatureLoader loading instances of given feature.
extension Printer  {

  static func consolePrinter() -> FeatureLoader {
    // There are multiple available implementations of the FeatureLoader
    // which correspond to a lifetime and loading behavior of implemented feature
    // based on its type.
    .disposable { (features: Features) -> Printer in
      // `features` is a container context which can be used
      // to retrieve any other features if needed.
        logMessage: { (message: String) in

If you have defined your feature interface and some implementation you can then register it within a scope inside a container to be available. All feature implementations should be defined when creating container tree root.

let features: Features = FeaturesRoot { (registry: inout FeaturesRegistry<RootFeaturesScope>) in
  // you can also define other scopes and its features in this context

Finally you can access any feature from the container.

let printer: Printer = try features.instance()

If requested feature was not defined in available registry or there was any issue with accessing its instance you will get full diagnostics required to track the error.

โŽก โš ๏ธ FeatureUndefined
โŽœ ๐Ÿ“บ FeatureUndefined
โŽœ ๐Ÿงต Context: 
โŽœ ๐Ÿ“ MQDo/Example.swift:36
โŽœ โœ‰๏ธ FeatureUndefined 
โŽœ ๐Ÿงฉ feature: Printer
โŽฃ โš ๏ธ FeatureUndefined


Copyright 2021-2023 Miquido

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at


Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Stars: 4
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.


Release Notes

Fix common patches in tests
1 week ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics