Dependency Injection
Properties
| Name | Description | Type | Default |
|---|
| container | ตัวแปรที่เก็บ Dependency ทั้งหมดของ Service ไว้ โดยสามารถเรียก Dependency ได้ผ่านตัวแปรนี้ | Container | Container ที่ Register Dependency ของ HttpService และ LocalDBService เอาไว้ |
Method Overview
| Name | Description | Return |
|---|
| register() | การ Register Dependency ของ Service ไว้ใน Container | void |
Method Details
static func register<T>(classProtocol: T.Type, classObject: T)
การ Register Dependency ของ Service ไว้ใน Container
Parameters
| Name | Description | Type |
|---|
| T | Class ของ Service | Class |
| classProtocol | Protocol ของ Class | T.Type |
| classObject | Object ของ Class | T |
Example
// Protocol
protocol YourServiceProtocol {
func yourFunction()
}
// Class
class YourService: NSObject, YourServiceProtocol {
static let sharedInstance = YourService()
func yourFunction() {}
}
// Register YourService
DI.register(classProtocol: YourServiceProtocol.self, classObject: YourService.sharedInstance)
// Get YourService from container
let yourService = DI.container.resolve(YourServiceProtocol.self)!
// Can call your function
yourService.yourFunction()