0
# Container Configuration
1
2
Core service registration and container configuration functionality. The Container class provides the primary interface for setting up dependency injection services with various registration patterns and service lifestyles.
3
4
## Capabilities
5
6
### Container Initialization
7
8
Creates a new dependency injection container with optional configuration for strict mode and scoped service handling.
9
10
```python { .api }
11
def __init__(self, *, strict: bool = False, scope_cls: Optional[Type[ActivationScope]] = None):
12
"""
13
Initialize a new Container instance.
14
15
Args:
16
strict: If True, prevents automatic resolution of unregistered types
17
scope_cls: Custom ActivationScope class for scoped services
18
"""
19
```
20
21
### Basic Service Registration
22
23
General-purpose service registration method that supports multiple registration patterns including type-to-type, instance, and factory registration.
24
25
```python { .api }
26
def register(self, obj_type, sub_type=None, instance=None, *args, **kwargs) -> Container:
27
"""
28
Register a service in the container.
29
30
Args:
31
obj_type: The type to register (base type or service key)
32
sub_type: Concrete implementation type (for interface registration)
33
instance: Specific instance to register (for singleton registration)
34
*args: Constructor arguments for the service
35
**kwargs: Constructor keyword arguments for the service
36
37
Returns:
38
Container instance for method chaining
39
"""
40
```
41
42
Usage examples:
43
44
```python
45
container = Container()
46
47
# Register concrete type
48
container.register(UserService)
49
50
# Register interface to implementation
51
container.register(IUserRepository, SQLUserRepository)
52
53
# Register with constructor arguments
54
container.register(DatabaseService, connection_string="localhost")
55
```
56
57
### Singleton Registration
58
59
Registers services with singleton lifetime - only one instance will be created and reused across all resolutions.
60
61
```python { .api }
62
def add_singleton(self, base_type: Type, concrete_type: Optional[Type] = None) -> Container:
63
"""
64
Register a service as singleton.
65
66
Args:
67
base_type: Base type or interface to register
68
concrete_type: Concrete implementation (defaults to base_type)
69
70
Returns:
71
Container instance for method chaining
72
"""
73
```
74
75
### Transient Registration
76
77
Registers services with transient lifetime - a new instance is created for every resolution request.
78
79
```python { .api }
80
def add_transient(self, base_type: Type, concrete_type: Optional[Type] = None) -> Container:
81
"""
82
Register a service as transient.
83
84
Args:
85
base_type: Base type or interface to register
86
concrete_type: Concrete implementation (defaults to base_type)
87
88
Returns:
89
Container instance for method chaining
90
"""
91
```
92
93
### Scoped Registration
94
95
Registers services with scoped lifetime - one instance per scope (typically per request or operation).
96
97
```python { .api }
98
def add_scoped(self, base_type: Type, concrete_type: Optional[Type] = None) -> Container:
99
"""
100
Register a service as scoped.
101
102
Args:
103
base_type: Base type or interface to register
104
concrete_type: Concrete implementation (defaults to base_type)
105
106
Returns:
107
Container instance for method chaining
108
"""
109
```
110
111
### Instance Registration
112
113
Directly registers a specific instance as a singleton service.
114
115
```python { .api }
116
def add_instance(self, instance: Any, declared_class: Optional[Type] = None) -> Container:
117
"""
118
Register a specific instance as a singleton service.
119
120
Args:
121
instance: The instance to register
122
declared_class: Type to register the instance as (defaults to instance type)
123
124
Returns:
125
Container instance for method chaining
126
"""
127
```
128
129
### Type Binding
130
131
Low-level method for binding types with explicit lifestyle specification.
132
133
```python { .api }
134
def bind_types(self, obj_type: Any, concrete_type: Any = None, life_style: ServiceLifeStyle = ServiceLifeStyle.TRANSIENT) -> Container:
135
"""
136
Bind types with explicit lifestyle specification.
137
138
Args:
139
obj_type: Base type to bind
140
concrete_type: Concrete implementation type
141
life_style: Service lifestyle (TRANSIENT, SCOPED, SINGLETON)
142
143
Returns:
144
Container instance for method chaining
145
"""
146
```
147
148
### Service Provider Building
149
150
Builds the optimized service provider for fast service resolution after all registrations are complete.
151
152
```python { .api }
153
def build_provider(self) -> Services:
154
"""
155
Build the service provider for efficient service resolution.
156
157
Returns:
158
Services instance for resolving registered services
159
"""
160
```
161
162
### Provider Property Access
163
164
Provides lazy access to the built service provider, building it on first access if needed.
165
166
```python { .api }
167
@property
168
def provider(self) -> Services:
169
"""
170
Get the service provider, building it if not already built.
171
172
Returns:
173
Services instance for resolving registered services
174
"""
175
```
176
177
### Container Inspection
178
179
Check if a type is registered in the container.
180
181
```python { .api }
182
def __contains__(self, item) -> bool:
183
"""
184
Check if a type is registered in the container.
185
186
Args:
187
item: Type to check for registration
188
189
Returns:
190
True if the type is registered, False otherwise
191
"""
192
```
193
194
### Factory Registration (Low-Level)
195
196
Low-level method for registering factory functions with explicit lifestyle specification.
197
198
```python { .api }
199
def register_factory(self, factory: Callable, return_type: Optional[Type], life_style: ServiceLifeStyle) -> None:
200
"""
201
Register a factory function with explicit lifestyle specification.
202
203
Args:
204
factory: Factory function to create service instances
205
return_type: Type that the factory returns (inferred if not provided)
206
life_style: Service lifestyle (TRANSIENT, SCOPED, SINGLETON)
207
"""
208
```
209
210
### Generic Resolution
211
212
Resolve services directly from the container (typically used during configuration phase).
213
214
```python { .api }
215
def resolve(self, obj_type: Union[Type[T], str], scope=None, *args, **kwargs) -> T:
216
"""
217
Resolve a service instance from the container.
218
219
Args:
220
obj_type: Type or name of service to resolve
221
scope: Optional activation scope for scoped services
222
*args: Additional constructor arguments
223
**kwargs: Additional constructor keyword arguments
224
225
Returns:
226
Instance of the requested service type
227
"""
228
```