mirror of
https://github.com/otaviocc/MicroContainer.git
synced 2026-02-19 19:15:25 +00:00
A tiny, dependency-free Dependency Injection (DI) container for Swift.
- Swift 100%
| .github/workflows | ||
| Sources/MicroContainer | ||
| Tests/MicroContainerTests | ||
| .gitignore | ||
| .swiftformat | ||
| .swiftlint.yml | ||
| LICENSE | ||
| Package.swift | ||
| README.md | ||
MicroContainer
A tiny, dependency-free Dependency Injection (DI) container for Swift.
- Minimal API surface
- Singleton (static) and factory (dynamic) lifetimes
- Factories receive the container for nested resolution
- Thread-safe registration and resolution
- Optional qualifiers (named registrations)
- Safer resolve variants and circular dependency detection
Installation
Add MicroContainer to your Package.swift dependencies:
dependencies: [
.package(url: "https://github.com/otaviocc/MicroContainer.git", from: "0.0.2")
]
Quick Start
let container = DependencyContainer()
// Register a singleton (cached on first resolve)
container.registerSingleton(ServiceProtocol.self) { _ in
Service()
}
// Register a factory (new instance each time)
container.registerFactory(Repository.self) { _ in
Repository()
}
// Resolve
let service: ServiceProtocol = container.resolve()
let repo: Repository = container.resolve()
Qualifiers (named registrations)
Register multiple implementations under the same type by using qualifier:
container.registerSingleton(Client.self, qualifier: "primary") { _ in
Client(baseURL: URL(string: "https://api.example.com")!)
}
container.registerSingleton(Client.self, qualifier: "staging") { _ in
Client(baseURL: URL(string: "https://staging.example.com")!)
}
let primary: Client = container.resolve(qualifier: "primary")
let staging: Client = container.resolve(qualifier: "staging")
Safer resolve variants
let maybeService: Service? = container.resolveOptional()
do {
let service: Service = try container.resolveOrThrow()
} catch DependencyContainer.ResolutionError.notRegistered(let type) {
print("Missing registration: \(type)")
}
Utilities
// Presence
container.contains(Service.self)
// Remove
container.unregister(Service.self)
// Reset all registrations and caches
container.reset()
// Warm up all singletons (instantiate eagerly)
container.warmSingletons()
Circular dependency detection
If a factory resolves a type that forms a cycle, MicroContainer detects it:
resolve()fatals with a readable dependency chainresolveOrThrow()throwsResolutionError.circularChain(chain:)
Thread-safety
All registration and resolution operations are thread-safe. Singleton creation is atomic.
License
MIT. See LICENSE.