forked from jetbasrawi/go.cqrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository.go
192 lines (158 loc) · 5.53 KB
/
repository.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2016 Jet Basrawi. All rights reserved.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package ycq
import (
"fmt"
"net/url"
"github.com/jetbasrawi/go.geteventstore"
)
// DomainRepository is the interface that all domain repositories should implement.
type DomainRepository interface {
//Loads an aggregate of the given type and ID
Load(aggregateTypeName string, aggregateID string) (AggregateRoot, error)
//Saves the aggregate.
Save(aggregate AggregateRoot, expectedVersion *int) error
}
// GetEventStoreCommonDomainRepo is an implementation of the DomainRepository
// that uses GetEventStore for persistence
type GetEventStoreCommonDomainRepo struct {
eventStore *goes.Client
eventBus EventBus
streamNameDelegate StreamNamer
aggregateFactory AggregateFactory
eventFactory EventFactory
}
// NewCommonDomainRepository constructs a new CommonDomainRepository
func NewCommonDomainRepository(eventStore *goes.Client, eventBus EventBus) (*GetEventStoreCommonDomainRepo, error) {
if eventStore == nil {
return nil, fmt.Errorf("Nil Eventstore injected into repository.")
}
if eventBus == nil {
return nil, fmt.Errorf("Nil EventBus injected into repository.")
}
d := &GetEventStoreCommonDomainRepo{
eventStore: eventStore,
eventBus: eventBus,
}
return d, nil
}
// SetAggregateFactory sets the aggregate factory that should be used to
// instantate aggregate instances
//
// Only one AggregateFactory can be registered at any one time.
// Any registration will overwrite the provious registration.
func (r *GetEventStoreCommonDomainRepo) SetAggregateFactory(factory AggregateFactory) {
r.aggregateFactory = factory
}
// SetEventFactory sets the event factory that should be used to instantiate event
// instances.
//
// Only one event factory can be set at a time. Any subsequent registration will
// overwrite the previous factory.
func (r *GetEventStoreCommonDomainRepo) SetEventFactory(factory EventFactory) {
r.eventFactory = factory
}
// SetStreamNameDelegate sets the stream name delegate
func (r *GetEventStoreCommonDomainRepo) SetStreamNameDelegate(delegate StreamNamer) {
r.streamNameDelegate = delegate
}
// Load will load all events from a stream and apply those events to an aggregate
// of the type specified.
//
// The aggregate type and id will be passed to the configured StreamNamer to
// get the stream name.
func (r *GetEventStoreCommonDomainRepo) Load(aggregateType, id string) (AggregateRoot, error) {
if r.aggregateFactory == nil {
return nil, fmt.Errorf("The common domain repository has no Aggregate Factory.")
}
if r.streamNameDelegate == nil {
return nil, fmt.Errorf("The common domain repository has no stream name delegate.")
}
if r.eventFactory == nil {
return nil, fmt.Errorf("The common domain has no Event Factory.")
}
aggregate := r.aggregateFactory.GetAggregate(aggregateType, id)
if aggregate == nil {
return nil, fmt.Errorf("The repository has no aggregate factory registered for aggregate type: %s", aggregateType)
}
streamName, err := r.streamNameDelegate.GetStreamName(aggregateType, id)
if err != nil {
return nil, err
}
stream := r.eventStore.NewStreamReader(streamName)
for stream.Next() {
switch err := stream.Err().(type) {
case nil:
break
case *url.Error, *goes.ErrTemporarilyUnavailable:
return nil, &ErrRepositoryUnavailable{}
case *goes.ErrNoMoreEvents:
return aggregate, nil
case *goes.ErrUnauthorized:
return nil, &ErrUnauthorized{}
case *goes.ErrNotFound:
return nil, &ErrAggregateNotFound{AggregateType: aggregateType, AggregateID: id}
default:
return nil, &ErrUnexpected{Err: err}
}
event := r.eventFactory.GetEvent(stream.EventResponse().Event.EventType)
//TODO: No test for meta
meta := make(map[string]string)
stream.Scan(event, &meta)
if stream.Err() != nil {
return nil, stream.Err()
}
em := NewEventMessage(id, event, Int(stream.EventResponse().Event.EventNumber))
for k, v := range meta {
em.SetHeader(k, v)
}
aggregate.Apply(em, false)
aggregate.IncrementVersion()
}
return aggregate, nil
}
// Save persists an aggregate
func (r *GetEventStoreCommonDomainRepo) Save(aggregate AggregateRoot, expectedVersion *int) error {
if r.streamNameDelegate == nil {
return fmt.Errorf("The common domain repository has no stream name delagate.")
}
resultEvents := aggregate.GetChanges()
streamName, err := r.streamNameDelegate.GetStreamName(typeOf(aggregate), aggregate.AggregateID())
if err != nil {
return err
}
if len(resultEvents) > 0 {
evs := make([]*goes.Event, len(resultEvents))
for k, v := range resultEvents {
//TODO: There is no test for this code
v.SetHeader("AggregateID", aggregate.AggregateID())
evs[k] = goes.NewEvent("", v.EventType(), v.Event(), v.GetHeaders())
}
streamWriter := r.eventStore.NewStreamWriter(streamName)
err := streamWriter.Append(expectedVersion, evs...)
switch e := err.(type) {
case nil:
break
case *goes.ErrConcurrencyViolation:
return &ErrConcurrencyViolation{Aggregate: aggregate, ExpectedVersion: expectedVersion, StreamName: streamName}
case *goes.ErrUnauthorized:
return &ErrUnauthorized{}
case *goes.ErrTemporarilyUnavailable:
return &ErrRepositoryUnavailable{}
default:
return &ErrUnexpected{Err: e}
}
}
aggregate.ClearChanges()
for k, v := range resultEvents {
if expectedVersion == nil {
r.eventBus.PublishEvent(v)
} else {
em := NewEventMessage(v.AggregateID(), v.Event(), Int(*expectedVersion+k+1))
r.eventBus.PublishEvent(em)
}
}
return nil
}