Coder Social home page Coder Social logo

iordacheclaudiu / kraken Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mahbub-morshed/kraken

0.0 2.0 0.0 174 KB

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

License: MIT License

Ruby 1.76% Objective-C 0.68% Shell 13.85% Swift 83.71%

kraken's Introduction

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

kraken's People

Contributors

sabirvirtuoso avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.