Giter Site home page Giter Site logo

kontena's Introduction

Kontena (コンテナ)

Version License Platform

IOC container in Swift

A simple Swift implementation of Service Locator / IOC container with limited DI functionality. Works well with Swift and Objective C classes.

Please check Demo project for a very basic example on how to use Kontena.

Additional notes

  • If you want to bind Swift protocol you must add @objc annotation.
  • Your bound object must inherit from NSObject to let the container automatically resolve dependencies.
  • Swift 1.2 is required to use this library.

Usage

Create the container

import Kontena

let container = Container() // init
let sharedContainer = Container.sharedInstance // as singleton

Merge two containers

// all bound objects from container2
// are merged and added to container1
container1.mergeWithContainer(container2)

Clear the container

container.clear()

Let the container automatically resolve dependencies of bound objects

container.resolveDependencies = true

Bind/register the instance/factory

@objc protocol SomeProtocol {}
class SomeBaseClass: NSObject {}
class SomeClass: SomeBaseClass, SomeProtocol {}

var instance = SomeClass()

// as singleton to the type of the instance
container.bind(instance)
// as singleton to the superclass type
container.bind(instance, toType: SomeBaseClass.self)
// as singleton to the protocol
container.bind(instance, toType: SomeProtocol.self)
// as singleton to the key
container.bind(instance, toKey: "some")

// factory = closure where the object is initialized
let factory = {
  () -> SomeClass in
  let some = SomeClass()
  // basic initialization
  return some
}

// to the type of the instance
container.bindFactory(factory)
// to the type of the instance
container.bindFactory(factory, toType: SomeBaseClass.self)
// to the protocol
container.bindFactory(factory, toType: SomeProtocol.self)
// to the key
container.bindFactory(factory, toKey: "some")

// you can bind factory as singleton as well
container.bindFactory(factory).asSingleton()

Resolve the object

Resolved object or nil is returned. If the factory was bound then object will be initialized lazily (for factories with the singleton scope - only at the first resolve call, for the prototype scope - at every resolve call).

If resolveDependencies was set to true then container will try to automatically resolve dependencies of the requested object (if they are already registered in the container and the requested object is a subclass of NSObject).

var resolvedByType = container.resolveType(SomeClass.self)
var resolvedByKey = container.resolveKey(key)

Using of custom operators

var instance = SomeClass()
var resolvedInstance: SomeClass?

// should be bound to the shared container
Container.sharedInstance.bind(instance)

// Resolve and inject from the container
-->resolvedInstance // prefix operator
resolvedInstance<-- // postfix operator

Installation

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

pod 'Kontena'

Author

Vadym Markov, [email protected]

License

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

kontena's People

Contributors

vadymmarkov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.