Code Monkey home page Code Monkey logo

swin's Introduction

Swin

How to...

1. Register dependency

1.1 Using class name

Swin.register(type: DemoClassProtocol.self, using: DemoClass.self) 

1.2 or, using blocks

Swin.register(type: DemoClassProtocol.self) {
    let instance = DemoClass()
    // do more configurations
    return instance
}

2. Create objects

2.1 With no args

let instanceObject = Swin.create(type: DemoClassProtocol.self)

2.2 or, with args

let instanceObject = Swin.create(type: DemoClassProtocol.self, args: [String: Any]())

3. How are dependencies resolved in above case?

3.1 When using class name to resolve the dependency - the class must implement SwinInitializer protocol. i.e. DemoClass should implement SwinInitializer protocol. For eg.

class DemoClass: DemoClassProtocol, SwinInitializer {
    class func create(_ args: [String : Any]?) -> Any? {
        return DemoClass()
    }
}

3.2 When using block notation to register dependency (1.2) - there is no additional requirement

4. How to replace & restore dependencies?

Swin allows you to create replacement tokens, to temporarily replace a predefined dependency. This is particularly useful when writing tests. In the example below both DemoClass and Democlass2 implements the DemoClassProtocol

Swin.register(type: DemoClassProtocol.self, using: DemoClass.self)

let replaceToken = Swin.replace { dependencies in
    dependencies.add(type: DemoClassProtocol.self) {
    return DemoClass2()
  }
}

let instance1 = Swin.create(type: DemoClassProtocol.self)
// instance1 object is of Type DemoClass2

// call restore() method to undo the replacement
replaceToken.restore()
let instance2 = Swin.create(type: DemoClassProtocol.self)
// instance2 object is of Type DemoClass

Future work

  1. Add args support for block style register method

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.