Injection
provide.foundation.hub.injection
¶
TODO: Add module docstring.
Classes¶
Functions¶
create_instance
¶
Create an instance with dependency injection.
Resolves constructor dependencies from the registry and instantiates the class. Allows overriding specific dependencies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
Class to instantiate |
required |
registry
|
Any
|
Registry containing dependencies |
required |
**overrides
|
Any
|
Explicitly provided dependencies (override registry) |
{}
|
Returns:
| Type | Description |
|---|---|
T
|
New instance of cls |
Example
service = create_instance(MyService, hub._component_registry)
Or with overrides:¶
service = create_instance( ... MyService, ... hub._component_registry, ... logger=custom_logger ... )
Source code in provide/foundation/hub/injection.py
injectable
¶
Mark a class as injectable for dependency injection.
This decorator indicates that the class follows DI patterns and can be automatically instantiated by the Hub's resolve() method.
The decorator: - Validates that init has type hints for all parameters - Marks the class as injectable for documentation purposes - Does not modify the class behavior (zero runtime overhead)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
Class to mark as injectable |
required |
Returns:
| Type | Description |
|---|---|
type[T]
|
The same class, marked as injectable |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If init lacks proper type hints |
Example
@injectable class UserService: ... def init(self, db: Database, cache: Cache): ... self.db = db ... self.cache = cache
Source code in provide/foundation/hub/injection.py
is_injectable
¶
Check if a class is marked as injectable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[Any]
|
Class to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if class is marked with @injectable decorator |
Source code in provide/foundation/hub/injection.py
register
¶
Register a dependency in the registry by type.
This is a convenience function for type-based registration, which is essential for dependency injection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
Any
|
Registry to register in |
required |
type_hint
|
type[T]
|
Type to register under |
required |
instance
|
T
|
Instance to register |
required |
name
|
str | None
|
Optional name (defaults to type name) |
None
|
Source code in provide/foundation/hub/injection.py
resolve_dependencies
¶
Resolve constructor dependencies from registry.
Inspects the class init signature and attempts to resolve each typed parameter from the registry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type[T]
|
Class to resolve dependencies for |
required |
registry
|
Any
|
Registry containing registered dependencies |
required |
allow_missing
|
bool
|
If True, skip missing dependencies instead of raising |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary mapping parameter names to resolved instances |
Raises:
| Type | Description |
|---|---|
NotFoundError
|
If a required dependency is not registered |
ValidationError
|
If dependency resolution fails |
Example
deps = resolve_dependencies(MyService, hub._component_registry)
Source code in provide/foundation/hub/injection.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | |