-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder.go
184 lines (150 loc) · 4.67 KB
/
builder.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
package di
import (
"reflect"
"github.com/dozm/di/errorx"
"github.com/dozm/di/reflectx"
"github.com/dozm/di/syncx"
)
type ContainerBuilder interface {
Add(...*Descriptor)
// Remove all the descriptors that the service type is t.
Remove(t reflect.Type)
Contains(t reflect.Type) bool
Build() Container
ConfigureOptions(func(*Options))
}
type containerBuilder struct {
descriptors []*Descriptor
optionsConfigurators []func(*Options)
}
func (b *containerBuilder) ConfigureOptions(f func(*Options)) {
b.optionsConfigurators = append(b.optionsConfigurators, f)
}
func (b *containerBuilder) Add(d ...*Descriptor) {
b.descriptors = append(b.descriptors, d...)
}
func (b *containerBuilder) Remove(t reflect.Type) {
descriptors := b.descriptors
j := 0
for _, d := range descriptors {
if d.ServiceType != t {
descriptors[j] = d
j++
}
}
b.descriptors = descriptors[:j]
}
func (b *containerBuilder) Contains(t reflect.Type) bool {
for _, d := range b.descriptors {
if d.ServiceType == t {
return true
}
}
return false
}
func (b *containerBuilder) builtInServices(c *container) {
csf := c.CallSiteFactory
csf.Add(ContainerType, &ContainerCallSite{})
csf.Add(ScopeFactoryType, newConstantCallSite(ScopeFactoryType, c.Root))
csf.Add(IsServiceType, newConstantCallSite(IsServiceType, csf))
}
func (b *containerBuilder) configureOptions(options *Options) {
for _, f := range b.optionsConfigurators {
f(options)
}
}
func (b *containerBuilder) Build() Container {
options := DefaultOptions()
b.configureOptions(&options)
c := &container{
CallSiteFactory: newCallSiteFactory(b.descriptors),
realizedServices: syncx.NewMap[reflect.Type, ServiceAccessor](),
}
c.Root = newEngineScope(c, true)
c.engine = c.createEngine()
b.builtInServices(c)
if options.ValidateScopes {
c.callSiteValidator = newCallSiteValidator()
}
if options.ValidateOnBuild {
errs := make([]error, 0)
for _, d := range b.descriptors {
if e := c.validateService(d); e != nil {
errs = append(errs, e)
}
}
if len(errs) > 0 {
panic(&errorx.AggregateError{Errors: errs})
}
}
return c
}
// Create a ContainerBuilder
func Builder() ContainerBuilder {
return &containerBuilder{}
}
// New a descriptor with instance
func Instance[T any](instance any) *Descriptor {
return NewInstanceDescriptor(reflectx.TypeOf[T](), instance)
}
// New a transient constructor descriptor
func Transient[T any](ctor any) *Descriptor {
return NewConstructorDescriptor(reflectx.TypeOf[T](), Lifetime_Transient, ctor)
}
// New a scoped constructor descriptor
func Scoped[T any](ctor any) *Descriptor {
return NewConstructorDescriptor(reflectx.TypeOf[T](), Lifetime_Scoped, ctor)
}
// New a singleton constructor descriptor
func Singleton[T any](ctor any) *Descriptor {
return NewConstructorDescriptor(reflectx.TypeOf[T](), Lifetime_Singleton, ctor)
}
// Add a transient service descriptor to the ContainerBuilder.
// T is the service type,
// cb is the ContainerBuilder,
// ctor is the constructor of the service T.
func AddTransient[T any](cb ContainerBuilder, ctor any) {
cb.Add(Transient[T](ctor))
}
// Add a scoped service descriptor to the ContainerBuilder.
// T is the service type,
// cb is the ContainerBuilder,
// ctor is the constructor of the service T.
func AddScoped[T any](cb ContainerBuilder, ctor any) {
cb.Add(Scoped[T](ctor))
}
// Add a singleton service descriptor to the ContainerBuilder.
// T is the service type,
// cb is the ContainerBuilder,
// ctor is the constructor of the service T.
func AddSingleton[T any](cb ContainerBuilder, ctor any) {
cb.Add(Singleton[T](ctor))
}
// Add an instance service descriptor to the ContainerBuilder.
// T is the service type,
// cb is the ContainerBuilder,
// the instance must be assignable to the service T.
func AddInstance[T any](cb ContainerBuilder, instance any) {
cb.Add(Instance[T](instance))
}
// New a transient factory descriptor
func TransientFactory[T any](factory Factory) *Descriptor {
return NewFactoryDescriptor(reflectx.TypeOf[T](), Lifetime_Transient, factory)
}
// New a scoped factory descriptor
func ScopedFactory[T any](factory Factory) *Descriptor {
return NewFactoryDescriptor(reflectx.TypeOf[T](), Lifetime_Scoped, factory)
}
// New a singleton factory descriptor
func SingletonFactory[T any](factory Factory) *Descriptor {
return NewFactoryDescriptor(reflectx.TypeOf[T](), Lifetime_Singleton, factory)
}
func AddTransientFactory[T any](cb ContainerBuilder, factory Factory) {
cb.Add(TransientFactory[T](factory))
}
func AddScopedFactory[T any](cb ContainerBuilder, factory Factory) {
cb.Add(ScopedFactory[T](factory))
}
func AddSingletonFactory[T any](cb ContainerBuilder, factory Factory) {
cb.Add(SingletonFactory[T](factory))
}