Part 5: Readmodels
When implementing a new EventHandler
, users often want to store the results of event processing within the EventHandler itself and make them available to the read side (QueryHandler
) or simply respond to specific events (Reactor
).
Here, we are presenting the best practices for implementing an EventHandler
that handles event processing. Just like with Command, Query, and Aggregate, we create a new package folder named readmodel
and create the file domain/aqua/readmodel/readmodel.go
within it.
go
// domain/aqua/readmodel/readmodel.go
package readmodel
type AquaReadmodel struct {
EventRepository *repo.EventRepository
state *state
metrics *metrics
mapByAquaUuid sync.Map
}
func NewAquaReadModel(EventRepository *repo.EventRepository) *AquaReadmodel {
rm := &AquaReadmodel{}
rm.EventRepository = EventRepository
rm.state = &state{}
rm.metrics = &metrics{}
rm.mapByAquaUuid = sync.Map{}
return rm
}