GithubHelp home page GithubHelp logo

apboyle / swinject Goto Github PK

View Code? Open in Web Editor NEW

This project forked from swinject/swinject

0.0 2.0 0.0 728 KB

Dependency injection framework for Swift

License: MIT License

Swift 93.84% Ruby 0.47% HTML 3.85% Objective-C 1.74% Shell 0.10%

swinject's Introduction

Swinject

Travis CI Carthage compatible CocoaPods Version License Platform Language Swift

Swinject is a lightweight dependency injection framework for Swift, inspired by Ninject, Autofac, Typhoon, and highly inspired by Funq.

Dependency injection (DI) is a software design pattern that implements Inversion of Control (IoC) for resolving dependencies. In the pattern, Swinject helps your app split into loosely-coupled components, which can be developed, tested and maintained more easily. Swinject is powered by the Swift generic type system and first class functions to define dependencies of your app simply and fluently.

Features

  • Pure Swift Type Injection
  • Initializer/Property/Method Injections
  • Initialization Callback
  • Circular Dependency Injection
  • Injection with Arguments
  • Self-registration (Self-binding)
  • Container Hierarchy
  • Object Scopes as None (Transient), Graph, Container (Singleton) and Hierarchy
  • Injection of both Reference and Value Types
  • Storyboard

Requirements

  • iOS 8.0+ / Mac OS X 10.10+
  • Xcode 7.0

Installation

Swinject is available through Carthage or CocoaPods.

Carthage

To install Swinject with Carthage, add the following line to your Cartfile.

github "Swinject/Swinject" ~> 0.2

Then run carthage update command. For details of the installation and usage of Carthage, visit its project page.

CocoaPods

To install Swinject with CocoaPods, add the following lines to your Podfile.

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'Swinject', '~> 0.2'

Then run pod install command. For details of the installation and usage of CocoaPods, visit its official website.

Documentation

All documentation can be found in the Documentation folder, including patterns of dependency injection and examples.

Basic Usage

First, register a service and component pair to a Container, where the component is created by the registered closure as a factory. In this example, Cat and PetOwner are component classes implementing AnimalType and PersonType service protocols, respectively.

let container = Container()
container.register(AnimalType.self) { _ in Cat(name: "Mimi") }
container.register(PersonType.self) { r in
     PetOwner(pet: r.resolve(AnimalType.self)!)
}

Then get an instance of a service from the container. The person is resolved to a pet owner, and playing with the cat named Mimi!

let person = container.resolve(PersonType.self)!
person.play() // prints "I'm playing with Mimi."

Where definitions of the protocols and classes are

protocol AnimalType {
    var name: String? { get }
}

class Cat: AnimalType {
    let name: String?

    init(name: String?) {
        self.name = name
    }
}

and

protocol PersonType {
    func play()
}

class PetOwner: PersonType {
    let pet: AnimalType

    init(pet: AnimalType) {
        self.pet = pet
    }

    func play() {
        let name = pet.name ?? "someone"
        print("I'm playing with \(name).")
    }
}

Notice that the pet of PetOwner is automatically set as the instance of Cat when PersonType is resolved to the instance of PetOwner. If a container already set up is given, you do not have to care what are the actual types of the services and how they are created with their dependency.

Play in Playground!

The project contains Sample-iOS.playground to demonstrate the features of Swinject. Download or clone the project, run the playground, modify it, and play with it to learn Swinject.

To run the playground in the project, first build the project, then select Editor > Execute Playground menu in Xcode.

Example Apps

  • SwinjectSimpleExample demonstrates dependency injection and Swinject in a simple weather app that lists current weather information at some locations.
  • SwinjectMVVMExample demonstrates dependency injection with Swift and reactive programming with ReactiveCocoa in MVVM architecture.

Blog Posts

The following blog posts introduce Swinject and the concept of dependency injection.

License

MIT license. See the LICENSE file for details.

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.