DIContainer
DIContainer copied to clipboard
DIContainer is lightweight dependency injection container framework for Swift
Dependency Injection Container
English | 한국어
Overview
The Swift Dependency Injection Container is a lightweight and flexible library designed to facilitate dependency management in Swift applications. It provides a structured and type-safe approach to resolve dependencies throughout your codebase, promoting code reusability, testability, and maintainability.
Features
- Type-safe dependency resolution.
- Lazy instantiation of dependencies.
- Property wrappers for convenient dependency injection.
- Dynamic module registration and management.
- Result builder syntax for declarative module registration.
- Debug utilities for module and injection key scanning.
Requirements
- Swift 5.9+
Installation
DIContainer is available Swift Package Manager.
Swift Package Manager
in Package.swift add the following:
dependencies: [
.package(url: "https://github.com/minsOne/DIContainer.git", from: "1.0.0")
]
Usage
Basic Usage
First, register a key and module pair to a Container, where the module has concreate type. Key has protocol type, and concreate type is inheriting protocol type
Container {
Module(AnimalKey.self) { Cat() }
}
.build()
Then get an instance from the container.
@Inject(AnimalKey.self)
var cat: Meow
cat.doSomething() // prints "Meow.."
Where definitions of the protocols and struct are
class AnimalKey: InjectionKey {
var type: Meow?
}
protocol Meow {
func doSomething()
}
struct Cat: Meow {
func doSomething() {
print("Meow..")
}
}
Test
$ (cd MockData/Sources/MockData && for i in {1..20}; do cp MockClass.swift MockClass$i.swift; done)
$ swift test
$ swift test -Xswiftc -O
Contributing
Contributions to the Swift Dependency Injection Container are welcome. Here are ways you can contribute:
- Reporting issues
- Suggesting enhancements
- Submitting pull requests with bug fixes or new features
Please ensure to follow the coding standards and write tests for new functionality.
License
This project is licensed under the MIT License.
Post
- [Swift 5.7+] Dependency Injection (1) - PropertyWrapper를 이용한 Service Locator 구현하기
- [Swift 5.7+] Dependency Injection (2) - 컨테이너 무결성 보장해 보기
- [Swift 5.7+][Objective-C] Dependency Injection (3) - objc_getClassList를 사용하여 모든 클래스 목록 얻기
Credits
The DIContainer are inspired by:
- Dependency Injection in Swift using latest Swift features
- iOS Dependency Injection Using Swinject
- Dependency Injection via Property Wrappers
- DI 라이브러리 “Koin” 은 DI가 맞을까?
- SwiftLee 방식의 DI를 하는 것으로 TCA의 Environment 버킷 릴레이를 그만두고 싶은 이야기
- Swift Dependency Injection via Property Wrapper
- 뱅크샐러드 안드로이드 앱에서 Koin 걷어내고 Hilt로 마이그레이션하기
- 마틴 파울러 - Inversion of Control Containers and the Dependency Injection pattern
- Nest.js는 실제로 어떻게 의존성을 주입해줄까?
- mikeash.com - Friday Q&A 2014-08-08: Swift Name Mangling
- Wikipedia - Name mangling
- Github - DerekSelander/dsdump
- Building a class-dump in 2020