SimpleDI : an easy to understand dependency injection (di) and inversion of control (ioc) container for swift.
- implemented in 1 class
- less than 100 lines of code
- readable syntax because of type inference
- contains all commonly used di/ioc features
- register/resolve services using an interface
- configure lifetime (singleton/transient)
- register/resolve fixed values
- dependency injection
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