Code Monkey home page Code Monkey logo

cupholder's Introduction

Cupholder

A lightweight Dependency Injection framework for Swift.

Features

  • Property injection
  • Constructor injection (through factories)
  • Fully tested (near 100% test coverage)
  • Context-aware dependency resolution
  • Simple to use
  • Swift 4.2

How to install

Cocoa Pods

Coming soon…

Swift Package Manager

Add the dependency to your project:

.package(url: "https://github.com/morpheby/Cupholder.git", from: "1.0.0")

From source

Compile and use as framework.

Usage

Register a class to protocol

  Inject<MyServiceType>().registration = Constructor(Shared({
      return MyDefaultService()
  }))

Use injection

Option 1: Use property injection with saved context

class MyDefaultOtherService: MyOtherServiceType, Injection {
    var injector: Injector!

    lazy var a: MyServiceType = injector.instanceOf().inject()
}

Option 2: Use property injection without saving context

class MyDefaultOtherService: MyOtherServiceType {
    var a: MyServiceType = InstanceOf<MyServiceType>().inject()
    // Don't use lazy in this case, since it will break DI context

Option 3: Use injection from context

class MyDefaultOtherService: MyOtherServiceType, Injection {
    var injector: Injector!

    func myType() -> MyServiceType {
        return injector.instanceOf().inject()
    }
}

Option 4: Injection from factory

class MyDefaultOtherService: MyOtherServiceType, Injection {
    var a: MyServiceType
    
    init(a: MyServiceType) { ... }
}

...

  Inject<MyOtherServiceType>().registration = Constructor(Shared({ injector in
      return MyDefaultOtherService(a: injector.instanceOf().inject())
  }))

Option 5: Injection from root global context

    var a: MyServiceType = InstanceOf<MyServiceType>().inject()

Note: You can freely combine those methods as you need

Update context

You can update current DI context with a custom object (i.e. some shared response from server):

class MyDefaultOtherService: MyOtherServiceType, Injection {
    var injector: Injector!

    func invokeSomething() -> MyServiceType {
        let myResponse = something.makeResponse()
        injector.inject().registration = Constructor(Instance<MyResponseType>(myResponse))
        return injector.instanceOf().inject()
    }
}

DI types

Supported types of registration are the following:

  • Unique: Object is created each time an injection is requested
  • Shared: Object is created only if it's currently doesn't already exist in scope (i.e. it is stored as a weak var). Otherwise a new object is created
  • Singleton: Object is created upon registration and later always reused
  • Lazy Singleton: Object is created upon first use and later always reused
  • Instance: Same as Singleton, only the object is passed itself to the registration (i.e. no factory is provided and the object itself bypasses dependency injection)

cupholder's People

Contributors

morpheby avatar

Watchers

 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.