Getting Started
Initialising Syringe​
Syringe's core is the Container
. It is the central touchpoint for all interactions with Syringe.
There are two types of Container
:
- An ordinary
Container
- A global scope
Container
Creating a global scope Container
is the preferred way as it enables the use of the free DSL functions Syringe provides.
Creating a global scope Container
:
import Syringe
class DependencyHandler {
init() {
injectSyringe {
modules {
// Modules go here
}
}
}
}
Creating Modules​
Modules are dependency lists in Syringe. Defining a module is simple:
let appModule = module {
// Dependencies go here
}
Adding Dependencies​
Adding dependencies to modules is done via the module DSL:
let appModule = module {
singleton { _ in YourType() }
factory { _ in Int.random(0..<5) }
}
Resolving Dependencies​
Resolving dependencies is done via either the module or application DSL:
class Service {
let value: Int
init(value: Int) {
self.value = value
}
}
let appModule = module {
// Resolving at module level
singleton { module in YourType(value: module.get()) }
factory { _ in Int.random(0..<5) }
}
class View {
// Resolving at global level
let service: Service = inject()!
init() {
}
}