-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediator.go
51 lines (40 loc) · 1.01 KB
/
mediator.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
package mediator
import "context"
var globalMediator *ContextMediator
type ContextMediator struct {
Container Container
}
type Options struct {
Func func(request IRequest)
}
func WithContext(ctx context.Context) Options {
return Options{
Func: func(request IRequest) {
request.WithContext(ctx)
},
}
}
type Mediator interface {
Send(request IRequest, options ...Options) (interface{}, error)
GetContainer() Container
}
func NewMediator() Mediator{
mediator := &ContextMediator{Container: NewContainer()}
globalMediator = mediator
return mediator
}
func (m *ContextMediator) GetContainer() Container {
return m.Container
}
func (m *ContextMediator) Send(request IRequest, options ...Options) (interface{}, error) {
for _, option := range options {
option.Func(request)
}
return m.Container.ExecuteRequest(request)
}
func Send(request IRequest, options ...Options) (interface{}, error) {
if globalMediator == nil {
return nil, NotExistsMediator
}
return globalMediator.Send(request, options...)
}