Simple Dependency Injection container for Swift. Use protocols to resolve dependencies with easy-to-use syntax!

Related tags

Core Data Kraken
Overview

Kraken

Build Status Code Climate Version License Platform Swift Version

Kraken GIF Photo courtesy of www.krakenstudios.blogspot.com

Introduction

Kraken is a simple Dependency Injection Container.

It's aimed to be as simple as possible yet provide rich functionality usual for DI containers on other platforms. It's inspired by Dip and some other DI containers.

  • You start by creating a Dependency Configurator for bootstrapping and registering your dependencies, by associating a protocol or type to either an implementation type, an implementation or a factory. It is preferrable to call your Dependency Configurator from main.swift.
  • Then you can call inject(typeToInject) to resolve an instance of protocol or type based on the bootstrapping in your Dependency Configurator.

Documentation

Kraken is yet to be documented fully but it comes with a sample project that lets you try all its features and become familiar with the API. You can find it in Trigger.xcworkspace.

File an issue if you have any question.

To run the example project, clone the repo, and run pod install from the Example directory first.

Features

  • Scopes. Kraken supports 3 different scopes (or life cycle strategies): Prototype, Singleton, EagerSingleton;
  • Named definitions. You can register an implementation type, an implementation or a factory for a protocol or type;
  • Runtime arguments. You can register factories that accept up to 3 runtime arguments (You can create an extension to increase number of runtime arguments);
  • Multiple definitions. You can register multiple implementation types, implementations or factories per type or protocol;
  • Circular dependencies. Kraken can resolve circular dependencies;
  • Auto-wiring. Kraken can infer your components' dependencies injected in constructor and automatically resolve them.
  • Easy configuration. No complex container hierarchy, no unneeded functionality;
  • Thread safety. Registering and resolving components is thread safe;
  • Helpful error messages. If something can not be resolved at runtime Kraken throws an error that completely describes the issue;

Basic usage

Create a Dependency Configurator file where you bootstrap your dependencies much like the example shown below:

import Kraken

class DependencyConfigurator {

  static func bootstrapDependencies() {

    // Register a protocol or type by providing its implementation type
    Kraken.register(ServiceA.self, implementationType: ServiceAImpl.self, scope: .singleton)

    // Register same protocol or type with different dependency by providing tag that can be either Int, String or Custom Type conforming to DependencyTagConvertible
    Kraken.register(ServiceA.self, tag: CustomTag.One, implementationType: CustomFactory.self, scope: .singleton)

    // Register a protocol or type by providing its implementation
    Kraken.register(ServiceC.self, implementation: dummyImplementation, scope: .singleton)

    // Register a protocol or type having weak property to allow Kraken to handle circular dependencies
    // An example of such protocol (ServiceB) is given below
    Kraken.register(ServiceB.self, implementationType: ServiceBImpl.self, scope: .singleton) {
      (resolvedInstance: Injectable) -> () in

      let serviceB = resolvedInstance as! ServiceBImpl
      serviceB.serviceA = injectWeak(ServiceA.self).value as! ServiceAImpl
    }

    // Register a protocol or type having runtime arguments to be injected in constructor
    Kraken.register(ServiceD.self) {
      ServiceDImpl(host: $0, port: $1, serviceB: inject(ServiceB.self)) as ServiceD
    }

    // Register generic protocols or types
    Kraken.register(GenericDataSource<ServiceAImpl>.self, implementationType: ServiceAImplDataSource.self, scope: .eagerSingleton)

    // Register a protocol or type whose components' dependencies are injected automatically by container
    Kraken.register(ServiceE.self) {
      ServiceEImpl(serviceA: $0, serviceB: $1, serviceC: $2)
    }
  }
}
import Kraken

protocol ServiceB: Injectable {

  weak var serviceA: ServiceA? { get set }

  var serviceC: ServiceC { get set }

  var serviceBImplDataSource: GenericDataSource<ServiceBImpl> { get set }

  func myCompanyB() -> String

}

It is worth mentioning that the protocols or types which are registered must conform to the Injectable protocol in order to be resolved by the container as shown in the example below:

import Kraken

protocol ServiceA: Injectable {

  func myCompanyA() -> String

}

If you want to register multiple dependencies per type or protocol, you have to register with tag conforming to DependencyTagConvertible. You can declare a custom enum for this as shown below:

import Kraken

enum CustomTag: Int, DependencyTagConvertible {
    case One = 1, Two
}

After bootstrapping dependencies, its injection is as simple as invoking inject() which can be of different types as shown below:

import Kraken

// Inject dependency whose implementation was registered
let serviceC: ServiceC = inject(ServiceC.self)

// Inject dependency whose implementation type was registered
let serviceA: ServiceA = inject(ServiceA.self)

// Inject dependency whose implementation type was registered with tag
let serviceA: ServiceA = inject(ServiceA.self, tag: CustomTag.One)

// Inject dependency providing runtime arguments
let serviceD: ServiceD = inject(ServiceD.self, withArguments: "localhost", 8080)

// Inject dependency which is resolved by container through AutoWiring
let serviceE: ServiceE = inject(ServiceE.self)

Installation

Kraken is built with Swift 3.0.

CocoaPods

Kraken is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Kraken', '1.5.0'

Manually

  1. Download and drop /Kraken folder in your project.
  2. Congratulations!

Author

Syed Sabir Salman-Al-Musawi, [email protected]

I'd also like to thank Sharafat Ibn Mollah Mosharraf for his big support during the development phase.

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

The GIF at the top of this README.md is from www.krakenstudios.blogspot.com

You might also like...
CoreData based Swift ORM
CoreData based Swift ORM

Swift ORM Features Pure swift objects - no more subclasses of NSManagedObject Extensible attribute system - store any type in CoreData storage by impl

Swift client library to interact with Supabase Storage
Swift client library to interact with Supabase Storage

storage-swift Swift Client library to interact with Supabase Storage.

HitList is a Swift App shows the implementation of Core Data.
HitList is a Swift App shows the implementation of Core Data.

HitList HitList is a Swift App shows the implementation of Core Data. It is the demo app of Ray Wenderlich's tech blog. For details please reference G

A type-safe, fluent Swift library for working with Core Data
A type-safe, fluent Swift library for working with Core Data

Core Data Query Interface (CDQI) is a type-safe, fluent, intuitive library for working with Core Data in Swift. CDQI tremendously reduces the amount o

JSON to Core Data and back. Swift Core Data Sync.
JSON to Core Data and back. Swift Core Data Sync.

Notice: Sync was supported from it's creation back in 2014 until March 2021 Moving forward I won't be able to support this project since I'm no longer

A Swift framework that wraps CoreData, hides context complexity, and helps facilitate best practices.
A Swift framework that wraps CoreData, hides context complexity, and helps facilitate best practices.

Cadmium is a Core Data framework for Swift that enforces best practices and raises exceptions for common Core Data pitfalls exactly where you make the

An NSPredicate DSL for iOS, OSX, tvOS, & watchOS. Inspired by SnapKit and lovingly written in Swift.
An NSPredicate DSL for iOS, OSX, tvOS, & watchOS. Inspired by SnapKit and lovingly written in Swift.

PrediKit A Swift NSPredicate DSL for iOS & OS X inspired by SnapKit, lovingly written in Swift, and created by that weird dude at KrakenDev. If you're

🎯 PredicateKit allows Swift developers to write expressive and type-safe predicates for CoreData using key-paths, comparisons and logical operators, literal values, and functions.
🎯 PredicateKit allows Swift developers to write expressive and type-safe predicates for CoreData using key-paths, comparisons and logical operators, literal values, and functions.

🎯 PredicateKit PredicateKit is an alternative to NSPredicate allowing you to write expressive and type-safe predicates for CoreData using key-paths,

This project server as a demo for anyone who wishes to learn Core Data in Swift.

CoreDataDemo This project server as a demo for anyone who wishes to learn Core Data in Swift. The purpose of this project is to help someone new to Co

Owner
Syed Sabir Salman-Al-Musawi
Syed Sabir Salman-Al-Musawi
Write amazing, strong-typed and easy-to-read NSPredicate.

PredicateFlow Write amazing, strong-typed and easy-to-read NSPredicate. This library allows you to write flowable NSPredicate, without guessing attrib

Andrea Del Fante 103 Aug 12, 2022
100% Swift Simple Boilerplate Free Core Data Stack. NSPersistentContainer

DATAStack helps you to alleviate the Core Data boilerplate. Now you can go to your AppDelegate remove all the Core Data related code and replace it wi

Nes 216 Jan 3, 2023
QueryKit, a simple type-safe Core Data query language.

QueryKit QueryKit, a simple type-safe Core Data query language. Usage QuerySet<Person>(context, "Person")

QueryKit 1.5k Dec 20, 2022
A simple application for retrieving lunch menus of selected restaurants around FIT CTU

lunch_guy-ios (in progress) A simple application for retrieving lunch menus of selected restaurants using https://github.com/tomaskadlec/lunch_guy API

Tomas Sykora, jr. 12 Feb 10, 2022
Simple IOS notes app written programmatically without storyboard using UIKit and CoreData

Notes Simple Notes app. Swift, UIKit, CoreData Description Simple IOS notes app

null 4 Dec 23, 2022
V2RayXS: A simple GUI for Xray on macOS

V2RayXS: A simple GUI for Xray on macOS

tzmax 272 Dec 31, 2022
Super awesome Swift minion for Core Data (iOS, macOS, tvOS)

⚠️ Since this repository is going to be archived soon, I suggest migrating to NSPersistentContainer instead (available since iOS 10). For other conven

Marko Tadić 306 Sep 23, 2022
A powerful and elegant Core Data framework for Swift.

A powerful and elegant Core Data framework for Swift. Usage Beta version. New docs soon... Simple do that: let query = persistentContainer.viewContext

null 782 Nov 6, 2022
CloudCore is a framework that manages syncing between iCloud (CloudKit) and Core Data written on native Swift.

CloudCore CloudCore is a framework that manages syncing between iCloud (CloudKit) and Core Data written on native Swift. Features Leveraging NSPersist

deeje cooley 123 Dec 31, 2022
Unleashing the real power of Core Data with the elegance and safety of Swift

Unleashing the real power of Core Data with the elegance and safety of Swift Dependency managers Contact Swift 5.4: iOS 11+ / macOS 10.13+ / watchOS 4

John Estropia 3.7k Jan 9, 2023