File tree 2 files changed +14
-0
lines changed
CodeEdit/Utils/DependencyInjection
2 files changed +14
-0
lines changed Original file line number Diff line number Diff line change 7
7
8
8
import Foundation
9
9
10
+ /// A service container that manages the registration and resolution of services.
10
11
enum ServiceContainer {
12
+ /// A dictionary storing the closures for creating service instances.
11
13
private static var factories : [ ObjectIdentifier : ( ) -> Any ] = [ : ]
14
+ /// A dictionary storing the cached service instances.
12
15
private static var cache : [ ObjectIdentifier : Any ] = [ : ]
16
+ /// A dispatch queue used for synchronizing access to the factories and cache.
13
17
private static let queue = DispatchQueue ( label: " ServiceContainerQueue " )
14
18
19
+ /// Registers a factory closure for creating instances of a service type.
20
+ ///
21
+ /// - Parameter factory: An autoclosure that returns an instance of the service type.
15
22
static func register< Service> ( _ factory: @autoclosure @escaping ( ) -> Service ) {
16
23
queue. sync {
17
24
let key = ObjectIdentifier ( Service . Type. self)
18
25
factories [ key] = factory
19
26
}
20
27
}
21
28
29
+ /// Resolves an instance of a service type based on the specified resolution type.
30
+ ///
31
+ /// - Parameters:
32
+ /// - resolveType: The type of resolution to use for the service. Defaults to `.singleton`.
33
+ /// - type: The type of the service to resolve.
34
+ /// - Returns: An instance of the resolved service type, or `nil` if the service is not registered.
22
35
static func resolve< Service> ( _ resolveType: ServiceType = . singleton, _ type: Service . Type ) -> Service ? {
23
36
let serviceId = ObjectIdentifier ( Service . Type. self)
24
37
Original file line number Diff line number Diff line change 5
5
// Created by Abe Malla on 4/3/24.
6
6
//
7
7
8
+ /// A property wrapper that provides access to a service instance.
8
9
@propertyWrapper
9
10
struct Service < Service> {
10
11
var service : Service
You can’t perform that action at this time.
0 commit comments