-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate_lifecycle.go
56 lines (50 loc) · 1.29 KB
/
aggregate_lifecycle.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 gddd
import (
"errors"
"google.golang.org/protobuf/reflect/protoreflect"
"strings"
"sync"
)
type aggregateLifecycle struct {
mutex sync.Mutex
domainEvents []DomainEvent
}
type aggregateChange = protoreflect.ProtoMessage
func (c *aggregateLifecycle) apply(agg Aggregate, aggChange aggregateChange) {
c.mutex.Lock()
if aggChange == nil {
c.mutex.Unlock()
panic(errors.New("aggregate apply failed, aggregateChange is nil"))
return
}
eventName := strings.TrimSpace(getAggregateChangeName(aggChange))
if eventName == "" {
c.mutex.Unlock()
panic(errors.New("aggregate apply failed, eventName is empty"))
return
}
domainEvent := SampleDomainEvent{
aggregateId: agg.Identifier(),
aggregateName: getAggregateName(agg),
eventName: eventName,
eventBody: aggChange,
}
domainEvent.initEventId()
err := handleAppliedDomainEvent(agg, &domainEvent)
if err != nil {
c.mutex.Unlock()
panic(errors.New("aggregate apply failed, apply domain event failed"))
return
}
c.domainEvents = append(c.domainEvents, &domainEvent)
c.mutex.Unlock()
return
}
func (c *aggregateLifecycle) getDomainEvents() []DomainEvent {
return c.domainEvents
}
func (c *aggregateLifecycle) cleanDomainEvents() {
c.mutex.Lock()
c.domainEvents = make([]DomainEvent, 0, 1)
c.mutex.Unlock()
}