0
# Service Resolution
1
2
Service activation and dependency resolution through the Services provider. The Services class provides efficient service lookup and instantiation with support for scoped resolution and method execution with dependency injection.
3
4
## Capabilities
5
6
### Services Initialization
7
8
Creates a new Services provider instance for service resolution.
9
10
```python { .api }
11
def __init__(self, services_map=None, scope_cls: Optional[Type[ActivationScope]] = None):
12
"""
13
Initialize a Services provider.
14
15
Args:
16
services_map: Internal service mapping (typically built by Container)
17
scope_cls: Custom ActivationScope class for scoped services
18
"""
19
```
20
21
### Service Resolution
22
23
Primary method for resolving services from the provider with optional scoping and default values.
24
25
```python { .api }
26
def get(self, desired_type: Union[Type[T], str], scope: Optional[ActivationScope] = None, *, default: Optional[Any] = ...) -> T:
27
"""
28
Get an instance of the specified service type.
29
30
Args:
31
desired_type: Type or name of service to resolve
32
scope: Optional activation scope for scoped services
33
default: Default value if service cannot be resolved (raises exception if not provided)
34
35
Returns:
36
Instance of the requested service type
37
38
Raises:
39
CannotResolveTypeException: If service cannot be resolved and no default provided
40
"""
41
```
42
43
Usage examples:
44
45
```python
46
# Build services from container
47
services = container.build_provider()
48
49
# Resolve a service
50
user_service = services.get(UserService)
51
52
# Resolve with scope
53
with services.create_scope() as scope:
54
scoped_service = services.get(ScopedService, scope)
55
56
# Resolve with default value
57
optional_service = services.get(OptionalService, default=None)
58
```
59
60
### Service Setting
61
62
Manually set a service instance in the provider.
63
64
```python { .api }
65
def set(self, new_type: Union[Type, str], value: Any):
66
"""
67
Manually set a service instance in the provider.
68
69
Args:
70
new_type: Type or name to associate with the value
71
value: Service instance to set
72
"""
73
```
74
75
### Scope Creation
76
77
Creates a new activation scope for managing scoped service lifetimes.
78
79
```python { .api }
80
def create_scope(self, scoped: Optional[Dict[Union[Type, str], Any]] = None) -> ActivationScope:
81
"""
82
Create a new activation scope for scoped services.
83
84
Args:
85
scoped: Optional dictionary of pre-scoped services
86
87
Returns:
88
New ActivationScope instance
89
"""
90
```
91
92
### Method Executor Creation
93
94
Creates an optimized executor function for methods with dependency injection.
95
96
```python { .api }
97
def get_executor(self, method: Callable) -> Callable:
98
"""
99
Create an optimized executor for a method with dependency injection.
100
101
Args:
102
method: Method to create executor for
103
104
Returns:
105
Optimized callable that automatically injects dependencies
106
"""
107
```
108
109
Usage example:
110
111
```python
112
def business_method(user_service: UserService, logger: Logger):
113
# Method implementation
114
pass
115
116
# Create executor
117
executor = services.get_executor(business_method)
118
119
# Execute with automatic dependency injection
120
result = executor()
121
```
122
123
### Method Execution
124
125
Execute a method with automatic dependency injection, optionally within a specific scope.
126
127
```python { .api }
128
def exec(self, method: Callable, scoped: Optional[Dict[Type, Any]] = None) -> Any:
129
"""
130
Execute a method with automatic dependency injection.
131
132
Args:
133
method: Method to execute
134
scoped: Optional dictionary of scoped services for this execution
135
136
Returns:
137
Return value of the executed method
138
"""
139
```
140
141
Usage examples:
142
143
```python
144
def handle_request(user_service: UserService, request_id: str):
145
return user_service.process_request(request_id)
146
147
# Execute with dependency injection
148
result = services.exec(handle_request, {"request_id": "12345"})
149
150
# Execute with scoped services
151
scoped_services = {RequestContext: RequestContext("current_request")}
152
result = services.exec(handle_request, scoped=scoped_services)
153
```
154
155
### Dictionary-like Interface
156
157
Services class provides dictionary-like access methods for interoperability.
158
159
```python { .api }
160
def __contains__(self, item):
161
"""Check if a service type is registered."""
162
163
def __getitem__(self, item):
164
"""Get a service instance using bracket notation."""
165
166
def __setitem__(self, key, value):
167
"""Set a service instance using bracket notation."""
168
```
169
170
Usage examples:
171
172
```python
173
# Check if service is registered
174
if UserService in services:
175
user_service = services[UserService]
176
177
# Set service using bracket notation
178
services[Logger] = ConsoleLogger()
179
180
# Get service using bracket notation
181
logger = services[Logger]
182
```