-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncDependencyResolving.swift
More file actions
78 lines (71 loc) · 4.23 KB
/
Copy pathAsyncDependencyResolving.swift
File metadata and controls
78 lines (71 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// AsyncDependencyResolving.swift
// DependencyInjection
//
// Created by Róbert Oravec on 17.12.2024.
//
import Foundation
/// A type that is able to resolve a dependency
public protocol AsyncDependencyResolving {
/// Resolve a dependency that was previously registered within the container
///
/// If the container doesn't contain any registration for a dependency with the given type, ``ResolutionError`` is thrown
///
/// - Parameters:
/// - type: Type of the dependency that should be resolved
func tryResolve<Dependency: Sendable>(type: Dependency.Type) async throws -> Dependency
/// Resolve a dependency with variable arguments that was previously registered within the container
///
/// Uses Swift parameter packs to support 1-3 arguments with a single method signature.
/// If the container doesn't contain any registration for a dependency with the given type
/// or if arguments of different types than expected are passed, ``ResolutionError`` is thrown
/// Argument matching is based on compile-time types, so `ConcreteType` and `any Protocol` are treated as different argument lists.
///
/// - Parameters:
/// - type: Type of the dependency that should be resolved
/// - arguments: Arguments that will be passed as input parameters to the factory method (1-3 arguments supported)
func tryResolve<Dependency: Sendable, each Argument: Sendable>(type: Dependency.Type, arguments: repeat each Argument) async throws -> Dependency
}
public extension AsyncDependencyResolving {
/// Resolve a dependency that was previously registered within the container
///
/// If the container doesn't contain any registration for a dependency with the given type, a runtime error occurs
///
/// - Parameters:
/// - type: Type of the dependency that should be resolved
func resolve<Dependency: Sendable>(type: Dependency.Type) async -> Dependency {
try! await tryResolve(type: type)
}
/// Resolve a dependency that was previously registered within the container. A type of the required dependency is inferred from the return type
///
/// If the container doesn't contain any registration for a dependency with the given type, a runtime error occurs
func resolve<Dependency: Sendable>() async -> Dependency {
await resolve(type: Dependency.self)
}
/// Resolve a dependency with variable arguments that was previously registered within the container
///
/// Uses Swift parameter packs to support 1-3 arguments with a single method signature.
/// If the container doesn't contain any registration for a dependency with the given type
/// or if arguments of different types than expected are passed, a runtime error occurs
/// Argument matching is based on compile-time types, so `ConcreteType` and `any Protocol` are treated as different argument lists.
///
/// - Parameters:
/// - type: Type of the dependency that should be resolved
/// - arguments: Arguments that will be passed as input parameters to the factory method (1-3 arguments supported)
func resolve<Dependency: Sendable, each Argument: Sendable>(type: Dependency.Type, arguments: repeat each Argument) async -> Dependency {
try! await tryResolve(type: type, arguments: repeat each arguments)
}
/// Resolve a dependency with variable arguments that was previously registered within the container.
/// The type of the required dependency is inferred from the return type.
///
/// Uses Swift parameter packs to support 1-3 arguments with a single method signature.
/// If the container doesn't contain any registration for a dependency with the given type
/// or if arguments of different types than expected are passed, a runtime error occurs
/// Argument matching is based on compile-time types, so `ConcreteType` and `any Protocol` are treated as different argument lists.
///
/// - Parameters:
/// - arguments: Arguments that will be passed as input parameters to the factory method (1-3 arguments supported)
func resolve<Dependency: Sendable, each Argument: Sendable>(arguments: repeat each Argument) async -> Dependency {
await resolve(type: Dependency.self, arguments: repeat each arguments)
}
}