0
# Object Graph Creation
1
2
Core functionality for creating and configuring dependency injection containers. Object graphs serve as the central point for providing instances with automatic dependency resolution through implicit and explicit bindings.
3
4
## Capabilities
5
6
### Object Graph Factory
7
8
Creates a new ObjectGraph instance with configurable dependency resolution behavior, module scanning, and binding specifications.
9
10
```python { .api }
11
def new_object_graph(
12
modules=finding.ALL_IMPORTED_MODULES,
13
classes=None,
14
binding_specs=None,
15
only_use_explicit_bindings=False,
16
allow_injecting_none=False,
17
configure_method_name='configure',
18
dependencies_method_name='dependencies',
19
get_arg_names_from_class_name=bindings.default_get_arg_names_from_class_name,
20
get_arg_names_from_provider_fn_name=providing.default_get_arg_names_from_provider_fn_name,
21
id_to_scope=None,
22
is_scope_usable_from_scope=lambda _1, _2: True,
23
use_short_stack_traces=True
24
):
25
"""
26
Creates a new object graph for dependency injection.
27
28
Parameters:
29
- modules: modules to search for classes for implicit bindings; defaults to all imported modules
30
- classes: specific classes for implicit bindings; if None, no classes
31
- binding_specs: BindingSpec subclasses for custom bindings; if None, no binding specs
32
- only_use_explicit_bindings: if True, only use explicit bindings (no implicit class discovery)
33
- allow_injecting_none: if True, allow None values to be injected
34
- configure_method_name: name of method in BindingSpec classes for configuration
35
- dependencies_method_name: name of method in BindingSpec classes for dependencies
36
- get_arg_names_from_class_name: function to extract argument names from class names
37
- get_arg_names_from_provider_fn_name: function to extract argument names from provider function names
38
- id_to_scope: mapping of scope IDs to Scope instances for custom scopes
39
- is_scope_usable_from_scope: function determining if one scope can be used from another
40
- use_short_stack_traces: if True, provide abbreviated stack traces for errors
41
42
Returns:
43
ObjectGraph instance for dependency injection
44
"""
45
```
46
47
### ObjectGraph Instance
48
49
The main dependency injection container that provides instances with automatic dependency resolution.
50
51
```python { .api }
52
class ObjectGraph(object):
53
def provide(self, cls):
54
"""
55
Provides an instance of the specified class with dependencies injected.
56
57
Parameters:
58
- cls: class to instantiate (not an instance)
59
60
Returns:
61
Instance of cls with dependencies injected
62
63
Raises:
64
Error: if instance of cls is not providable
65
"""
66
```
67
68
## Usage Examples
69
70
### Basic Object Graph
71
72
```python
73
import pinject
74
75
class DatabaseService(object):
76
def __init__(self):
77
self.connection = "database connection"
78
79
class UserService(object):
80
def __init__(self, database_service):
81
self.db = database_service
82
83
# Create object graph with automatic module scanning
84
obj_graph = pinject.new_object_graph()
85
86
# Provide instance with automatic dependency injection
87
user_service = obj_graph.provide(UserService)
88
print(user_service.db.connection) # "database connection"
89
```
90
91
### Object Graph with Specific Classes
92
93
```python
94
import pinject
95
96
class ServiceA(object):
97
def __init__(self):
98
pass
99
100
class ServiceB(object):
101
def __init__(self, service_a):
102
self.service_a = service_a
103
104
# Create object graph with specific classes only
105
obj_graph = pinject.new_object_graph(classes=[ServiceA, ServiceB])
106
service_b = obj_graph.provide(ServiceB)
107
```
108
109
### Object Graph with Explicit Bindings Only
110
111
```python
112
import pinject
113
114
class Config(object):
115
def __init__(self):
116
self.setting = "production"
117
118
class MyBindingSpec(pinject.BindingSpec):
119
def configure(self, bind):
120
bind('config').to_instance(Config())
121
122
# Only use explicit bindings
123
obj_graph = pinject.new_object_graph(
124
binding_specs=[MyBindingSpec()],
125
only_use_explicit_bindings=True
126
)
127
```
128
129
### Object Graph with Custom Scopes
130
131
```python
132
import pinject
133
134
class CustomScope(pinject.Scope):
135
def provide(self, binding_key, default_provider_fn):
136
# Custom scoping logic
137
return default_provider_fn()
138
139
custom_scope_id = "custom"
140
obj_graph = pinject.new_object_graph(
141
id_to_scope={custom_scope_id: CustomScope()}
142
)
143
```