-
Notifications
You must be signed in to change notification settings - Fork 2
/
container.go
207 lines (188 loc) · 6.65 KB
/
container.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
package ioc
// Container is an inversion of control container.
type Container struct {
root *Container
*Values
r *registry
instances *Values
}
//-----------------------------------------------
// ctor
//-----------------------------------------------
// NewContainer creates a new inversion of control container.
func NewContainer() *Container {
return &Container{
Values: NewValues(),
r: newRegistry(),
instances: NewValues(),
}
}
// Scope creates a new scoped container from the current container.
//
// The Values of the current container are scoped and the registry inherited by the scoped container.
//
// Scoped Values will resolve an instance from an ancestor when the current container is unable to resolve the instance by type and name.
func (c *Container) Scope() *Container {
root := c
if c.root != nil {
root = c.root
}
return &Container{
root: root,
Values: NewValuesScope(c.Values),
r: c.r,
instances: NewValues(),
}
}
//-----------------------------------------------
// registry implementation
//-----------------------------------------------
// Returns the registrations for the container.
func (c *Container) Registrations() []*Registration {
return c.r.getAll()
}
// Register an instance factory with a specific lifetime.
//
// Register calls RegisterNamed(createInstance, implType, "", lifetime).
func (c *Container) Register(createInstance func(Factory) (interface{}, error), implType interface{}, lifetime Lifetime) error {
return c.RegisterNamed(createInstance, implType, "", lifetime)
}
// Register an instance factory with a specific lifetime.
//
// MustRegister calls Register(createInstance, implType, lifetime) and panics if an error is returned.
func (c *Container) MustRegister(createInstance func(Factory) (interface{}, error), implType interface{}, lifetime Lifetime) {
if err := c.Register(createInstance, implType, lifetime); err != nil {
panic(err)
}
}
// Register a named instance factory with a specific lifetime.
//
// Returns an error when:
// - The factory function is nil. (createInstance)
// - The implementing type is nil.
// - The implementing type isn't a pointer.
// - The instance lifetime isn't supported. Currently only PerContainer, PerScope and PerRequest lifetimes are supported.
func (c *Container) RegisterNamed(createInstance func(Factory) (interface{}, error), implType interface{}, name string, lifetime Lifetime) error {
typ, err := GetNamedType(implType, name)
if err != nil {
return err
}
if createInstance == nil {
return errCreateInstanceFnNil(typ, name)
}
registration := &Registration{
Type: typ,
Name: name,
CreateInstanceFn: createInstance,
Lifetime: lifetime,
}
// must keep the Lifetime check in sync with dependencyResolver.ResolveNamed
if lifetime != PerContainer && lifetime != PerScope && lifetime != PerRequest {
return errUnsupportedLifetime(registration.Type, registration.Name, lifetime)
}
c.r.set(typ, name, registration)
return nil
}
// Register a named instance factory with a specific lifetime.
//
// MustRegisterNamed calls RegisterNamed(createInstance, implType, name, lifetime) and panics if an error is returned.
func (c *Container) MustRegisterNamed(createInstance func(Factory) (interface{}, error), implType interface{}, name string, lifetime Lifetime) {
if err := c.RegisterNamed(createInstance, implType, name, lifetime); err != nil {
panic(err)
}
}
// Register an instance on the root container.
//
// RegisterInstance calls RegisterNamedInstance(v, "").
func (c *Container) RegisterInstance(v interface{}) error {
return c.RegisterNamedInstance(v, "")
}
// Register an instance on the root container.
//
// MustRegisterInstance calls RegisterInstance(v) and panics if an error is returned.
func (c *Container) MustRegisterInstance(v interface{}) {
if err := c.RegisterInstance(v); err != nil {
panic(err)
}
}
// Register a named instance on the root container.
//
// Returns an error when:
// - The instance type is nil.
// - The instance is a nil pointer or interface.
func (c *Container) RegisterNamedInstance(v interface{}, name string) error {
instance, err := GetNamedInstance(v, name)
if err != nil {
return err
}
typ := instance.Type()
createInstance := func(Factory) (interface{}, error) {
return v, nil
}
registration := &Registration{
Type: typ,
Name: name,
Value: v,
CreateInstanceFn: createInstance,
Lifetime: PerContainer,
}
c.r.set(typ, name, registration)
root := c.root
if root == nil {
root = c
}
root.instances.set(typ, name, instance)
return nil
}
// Register a named instance on the root container.
//
// MustRegisterNamedInstance calls RegisterNamedInstance(v, name) and panics if an error is returned.
func (c *Container) MustRegisterNamedInstance(v interface{}, name string) {
if err := c.RegisterNamedInstance(v, name); err != nil {
panic(err)
}
}
//-----------------------------------------------
// factory implementation
//-----------------------------------------------
// Resolve an instance by type.
//
// Resolve calls c.ResolveNamed(v, "").
func (c *Container) Resolve(v interface{}) error {
return c.ResolveNamed(v, "")
}
// Resolve an instance by type.
//
// MustResolve calls Resolve(v) and panics if an error is returned.
func (c *Container) MustResolve(v interface{}) {
if err := c.Resolve(v); err != nil {
panic(err)
}
}
// Resolve a named instance by type.
//
// ResolveNamed creates a dependency resolver implementing the Factory interface, that proxies resolve calls to the Container.
//
// The dependency resolver is passed to instance factory functions (instead of the container) and keeps track
// of the resolve call history for the request to detect infinite recursion.
//
// Returns an error when:
// - The value type is nil.
// - The value isn't a pointer.
// - The value is a nil pointer e.g. (*string)(nil) (use a pointer to a (nil) pointer instead)
// - The dependency can't be resolved (not registered).
// - The instance lifetime isn't supported. Currently only PerContainer, PerScope and PerRequest lifetimes are supported.
// - An error was returned when (*Registration).CreateInstance was called.
// - Infinite recursion is detected on a repetitive call to resolve an instance by type and name.
func (c *Container) ResolveNamed(v interface{}, name string) error {
resolver := newDependencyResolver(c, newDependencyResolverGraph())
return resolver.ResolveNamed(v, name)
}
// Resolve a named instance by type.
//
// MustResolveNamed calls ResolveNamed and panics if an error is returned.
func (c *Container) MustResolveNamed(v interface{}, name string) {
if err := c.ResolveNamed(v, name); err != nil {
panic(err)
}
}