ryan-mars / stochastic

TypeScript framework for building event-driven services. Easily go from Event Storming → Code.
MIT License
6 stars 1 forks source link

Store (Aggregate) #11

Open ryan-mars opened 3 years ago

ryan-mars commented 3 years ago

Maybe later:

“The Aggregate”

I kept the name ‘aggregate’ mostly for historical reasons, EventStorming was born around Domain-Driven Design and Aggregates, so it makes little sense to look for another name. Aggregates are unit of transactional consistency within a software system. More interestingly they are little state machines that expose consistent behaviour. — Alberto Brandolini

Aggregate

Illustration ©️ Avanscoperta

ryan-mars commented 3 years ago

Aggregate constructor should require the reduce function for the ordered list of events as well as the initialValue. The reducer will need to know the types of events it may receive, so we will need to add events. Adding the events to the aggregate here might simplify the EventStorm constructor which can take a very long list of components.

export const FlightScheduleAggregate = new Aggregate({
  __filename,
  key: "flightNo",
  shape: FlightSchedule,
  events: [ ... ] // Event components for this aggregate.
  reducer: (accumulator, event, index, array) => { ... }, 
  initialValue: { ... }
});
ryan-mars commented 3 years ago

Aggregate.Client provided to Command's Command.Handler (or execute) allows you to fetch an Aggregate by its key. It should provide more than just state (after reduce has run) it should allow access to the array of events/snapshots as well. Not sure what this should look like but some commands may need to see the list of events for handling difficult situations.

Currently it looks like this.

{
  get: (key) => Promise<some shape>
}

Perhaps the return of get could be {state: ... , events: [ ... ] }?

{
  get: (key) => Promise<{ state: ... , events: [ ... ] }>
}
ryan-mars commented 3 years ago

Thought: Commands and Domain Events mostly operate in relation to an Aggregate. Would it not make more sense to have a helper on an aggregate for declaring new Commands (setting the aggregate prop) and new Domain Events (defaulting the source key).