-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
44 lines (36 loc) · 954 Bytes
/
provider.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
package beans
import "reflect"
func(c *ProviderContainer) AddProvider(fun interface{}) {
c.createBean(fun,false,false)
}
func(c *ProviderContainer) createBean(data interface{},singleton,obj bool,cleanUpFunc ...CleanUpFunc) {
var (
returnType reflect.Type
beanData *bean
err error
)
if obj {
returnType,beanData,err = newBeanFromObjectData(data,cleanUpFunc...)
}else {
returnType,beanData,err = newBean(data,c,singleton)
}
if err != nil {
panic(err.Error())
}
_,ok := c.dependencyInitiator[returnType]
if ok {
panic(depAlreadyDefined)
}
c.dependencyInitiator[returnType] = beanData
}
func(c *ProviderContainer) AddProviderSingleton(fun interface{}) {
c.createBean(fun,true,false)
}
func(c *ProviderContainer) AddObjectSingleton(obj interface{},cleanUpFunc ...CleanUpFunc) {
c.createBean(obj,true,true,cleanUpFunc...)
}
func(c *ProviderContainer) CleanUp() {
for _,v := range c.dependencyInitiator {
v.cleanUp()
}
}