-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate_store.go
35 lines (29 loc) · 863 Bytes
/
aggregate_store.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
package es
import (
"context"
"github.com/ibiscum/Event-Driven-Architecture-in-Golang/Chapter11/internal/ddd"
)
type EventSourcedAggregate interface {
ddd.IDer
AggregateName() string
ddd.Eventer
Versioner
EventApplier
EventCommitter
}
type AggregateStoreMiddleware func(store AggregateStore) AggregateStore
type AggregateStore interface {
Load(ctx context.Context, aggregate EventSourcedAggregate) error
Save(ctx context.Context, aggregate EventSourcedAggregate) error
}
func AggregateStoreWithMiddleware(store AggregateStore, mws ...AggregateStoreMiddleware) AggregateStore {
s := store
// middleware are applied in reverse; this makes the first middleware
// in the slice the outermost i.e. first to enter, last to exit
// given: store, A, B, C
// result: A(B(C(store)))
for i := len(mws) - 1; i >= 0; i-- {
s = mws[i](s)
}
return s
}