Skip to content

Latest commit

 

History

History
53 lines (38 loc) · 1.57 KB

README.md

File metadata and controls

53 lines (38 loc) · 1.57 KB

SimpleDi easy to understand DI and IoC for swift

SimpleDI : an easy to understand dependency injection (di) and inversion of control (ioc) container for swift.

creating a container:

    let c = Container()

registering/resolving simple types:

    c.registerInstance(42)  //registers an Int
    c.register({ 42 }) //registers an Int 'lazyly'
    42 == c.resolve() //returns true

registering/resolving a service using protocol:

    c.register({ MyClass() as MyProtocol }) //registers concrete class as protocol
    c.resolve()! as MyProtocol //resolves instance of MyClass
    c.resolve()! is MyClass //evaluates to true

dependency injection:

    contaienr.register({ c in MyClass(c.resolve(), arg2: c.resolve(), arg3: c.resolve()) }) //dependency injection
    let cl = contaienr.resolve()! as MyClass //resolves the whole dependency tree

registering a non-singleton class:

    c.register({ MyClass() }, singleton: false) //registers as transient class
    let p = c.resolve()! as MyClass
    let p2 = c.resolve()! as MyClass
    p !== p2 //returns true