-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate.go
47 lines (38 loc) · 895 Bytes
/
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
package gddd
import "context"
func NewAggregateId() (id int64) {
id = node.Generate().Int64()
return
}
func ApplyAggregateChange(ctx context.Context, aggregate Aggregate, change aggregateChange) {
aggregate.Apply(aggregate, change)
return
}
type Aggregate interface {
InitId()
Identifier() (id int64)
Apply(agg Aggregate, event aggregateChange)
Applied() (events []DomainEvent)
}
type AbstractAggregate struct {
Id int64 `json:"id"`
lifecycle aggregateLifecycle
}
func (a *AbstractAggregate) InitId() {
if a.Id == 0 {
a.Id = NewAggregateId()
}
return
}
func (a *AbstractAggregate) Identifier() (id int64) {
id = a.Id
return
}
func (a *AbstractAggregate) Apply(agg Aggregate, aggChange aggregateChange) {
a.lifecycle.apply(agg, aggChange)
return
}
func (a *AbstractAggregate) Applied() (events []DomainEvent) {
events = a.lifecycle.getDomainEvents()
return
}