Event

The Event is something that happened in the domain.

One Event or many Events are the result of the command execution in an Aggregate and each can cause a state change in the Aggregate. They are immutable objects, because they represent undeniable facts. The storage of Events is organized an Event Stream, each stream contains all Events of an Aggregate.

Event Attributes:

  • Name: An identifier for what happens in the domain, written in past tense. The name is unique within an Aggregate.
  • Payload: Additional information about what it happened in the domain.
  • Occurred At: The time when it happened.
  • Sequence No: The order of all Events for an Aggregate.
  • Version: The order of all Events for an Aggregate ID.

Plan Event

Payload

Name

The Name is used to name the EventClass and forms the Event name.

For more details, see Naming.

Schema

The properties of the Event Payload are planned with the Codebricks Planner.

For more details, see Schema.

Value Sources

The Event Schema Property Value Sources define how each property is initialized.

For more details, see Data Flow.

Implement Event

An example event class is located at: Task/src/shared/domain/events/TaskAddedEvent.ts

1import { Event } from "@codebricks/typebricks";
2
3export interface TaskAddedEventProperties {
4 id: string;
5 aggregateId: string;
6 aggregateVersion: number;
7 payload: TaskAddedEventPayload;
8 occurredAt: Date;
9}
10
11export interface TaskAddedEventPayload {
12 title: string;
13 description: string;
14 assigneeId: string;
15}
16
17export class TaskAddedEvent extends Event<TaskAddedEventPayload> {
18 static readonly eventName: string = 'TaskAdded';
19
20 constructor(properties: TaskAddedEventProperties) {
21 super({...properties, name: TaskAddedEvent.eventName});
22 }
23}

Event Properties

This interface defines the properties of the TaskAddedEvent.

1export interface TaskAddedEventProperties {
2 id: string;
3 aggregateId: string;
4 aggregateVersion: number;
5 payload: TaskAddedEventPayload;
6 occurredAt: Date;
7}

Event Payload

This interface defines the payload structure for the TaskAddedEvent.

1export interface TaskAddedEventPayload {
2 title: string;
3 description: string;
4 assigneeId: string;
5}

Event Class

The TaskAddedEvent class extends the base Event class provided by the Codebricks framework.

1import { Event } from "@codebricks/codebricks-framework";
2
3export class TaskAddedEvent extends Event<TaskAddedEventPayload> {
4 static readonly eventName: string = 'TaskAdded';
5
6 constructor(properties: TaskAddedEventProperties) {
7 super({...properties, name: TaskAddedEvent.eventName});
8 }
9}

Best Practices

  • Event Name: The static eventName property ensures the event is named consistently.
  • Constructor: Initializes the event with the given properties and sets the event name.
  • Immutability: Events are immutable, ensuring they cannot be altered once created.

© 2024 Codebricks | All rights reserved.