-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate.go
56 lines (45 loc) · 997 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
48
49
50
51
52
53
54
55
56
package eventsource
import "context"
type Aggregate[T, A any] struct {
id A
data T
changes []Event[T]
version int
}
func (a *Aggregate[T, A]) ID() A {
return a.id
}
func (a *Aggregate[T, A]) On(e Event[T], isNew bool) {
e.Apply(&a.data)
if !isNew {
a.version++
}
}
type aggregateIDKey struct{}
func (a *Aggregate[T, A]) context(ctx context.Context) context.Context {
return context.WithValue(ctx, aggregateIDKey{}, a.id)
}
func AggregateID[A any](ctx context.Context) A {
id, _ := ctx.Value(aggregateIDKey{}).(A)
return id
}
func (a *Aggregate[T, A]) Execute(ctx context.Context, cmd Command[T]) error {
evts, err := cmd.Execute(a.context(ctx), a.data)
if err != nil {
return err
}
for _, e := range evts {
a.changes = append(a.changes, e)
a.On(e, true)
}
return nil
}
func (a *Aggregate[T, A]) Events() []Event[T] {
return a.changes
}
func (a *Aggregate[T, A]) Version() int {
return a.version
}
func (a *Aggregate[T, A]) Data() T {
return a.data
}