-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
55 lines (45 loc) · 1.5 KB
/
cmd.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
package hexago
import "fmt"
// Cmd is just Command's abbreviation, in the clean architecture there's a layer
// which is responsible for handling system use cases. It is common to call this
// layer `application` layer. It acts like an orchestrator which connects
// different layers. A common pattern for implementing the application layer is,
// Command Query Responsibility Segregation or CQRS. The Cmd struct, tries to act
// like a command.
type Cmd struct {
Name string
Payload interface{}
}
// NewCmd creates a new Cmd.
func NewCmd(name string, payload interface{}) Cmd {
return Cmd{
Name: name,
Payload: payload,
}
}
// CmdHandler is considered for being responsible to handling, one single Cmd
// [command]. It's common to use the domain repositories in order to ship
// aggregates, and using the domain functionalities to handling use cases.
type CmdHandler interface {
Handle(Cmd) (string, error)
}
// CmdHandlerFactory is a function that creates CmdHandler.
type CmdHandlerFactory func() CmdHandler
// CmdBag just holds a mapping between Cmd and CmdHandlerFactory
type CmdBag struct {
conf map[string]CmdHandlerFactory
}
// NewCmdBag creates an empty CmdBag.
func NewCmdBag() *CmdBag {
return &CmdBag{make(map[string]CmdHandlerFactory)}
}
func (cb *CmdBag) Add(cmd string, f CmdHandlerFactory) {
cb.conf[cmd] = f
}
func (cb *CmdBag) FactoryFor(cmd string) (CmdHandlerFactory, error) {
f, prs := cb.conf[cmd]
if !prs {
return nil, fmt.Errorf("command %s is not added yet", cmd)
}
return f, nil
}