forked from mishudark/eventhus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate.go
56 lines (48 loc) · 1.35 KB
/
aggregate.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
package eventhus
// BaseAggregate contains the basic info
// that all aggregates should have
type BaseAggregate struct {
ID string
Type string
Version int
Changes []Event
}
// AggregateHandler defines the methods to process commands
type AggregateHandler interface {
// LoadsFromHistory(events []Event)
ApplyChange(event Event)
ApplyChangeHelper(aggregate AggregateHandler, event Event, commit bool)
HandleCommand(Command) error
Uncommited() []Event
ClearUncommited()
IncrementVersion()
GetID() string
}
// Uncommited return the events to be saved
func (b *BaseAggregate) Uncommited() []Event {
return b.Changes
}
// ClearUncommited the events
func (b *BaseAggregate) ClearUncommited() {
b.Changes = []Event{}
}
// IncrementVersion ads 1 to the current version
func (b *BaseAggregate) IncrementVersion() {
b.Version++
}
// ApplyChangeHelper increments the version of an aggregate and apply the change itself
func (b *BaseAggregate) ApplyChangeHelper(aggregate AggregateHandler, event Event, commit bool) {
// increments the version in event and aggregate
b.IncrementVersion()
// apply the event itself
aggregate.ApplyChange(event)
if commit {
event.Version = b.Version
_, event.Type = GetTypeName(event.Data)
b.Changes = append(b.Changes, event)
}
}
// GetID of the current aggregate
func (b *BaseAggregate) GetID() string {
return b.ID
}