Entities and domain events are the two core building blocks of HexCore’s domain model.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Indroic/HexCore/llms.txt
Use this file to discover all available pages before exploring further.
BaseEntity provides identity, lifecycle timestamps, and an event registry so that your aggregates can raise business events without coupling to any infrastructure. DomainEvent and its typed subclasses give those events a consistent, immutable shape. This page explains all fields and methods, then walks through real examples.
BaseEntity
BaseEntity lives in hexcore.domain.base and is a Pydantic BaseModel configured for DDD usage — it supports ORM attribute reading (from_attributes=True) and re-validates fields on assignment (validate_assignment=True).
Fields
Universally unique identifier for the entity. Generated automatically when the entity is created. Never changes after construction.
UTC timestamp of when the entity was first created. Set automatically on construction.
UTC timestamp of the most recent update. Update this field in your entity methods when state changes.
Soft-delete flag. Active entities have
is_active=True. Calling deactivate() sets this to False without removing the record from the database.Methods
Appends a
DomainEvent instance to the entity’s internal event list. Call this inside entity mutation methods to record that something meaningful happened.Returns all registered events and clears the internal list. The Unit of Work calls this method to collect events before dispatching them. After calling
pull_domain_events() the entity holds no pending events.Discards all pending events without returning them. Useful in tests when you want to reset state without dispatching anything.
Async method that sets
is_active = False. Represents a logical (soft) delete. The entity is not removed from the database — queries should filter by is_active=True to exclude deactivated entities.Defining an entity
DomainEvent
DomainEvent lives in hexcore.domain.events. It is a frozen Pydantic model — all events are immutable once created.
Fields
Unique identifier for this specific event occurrence. Auto-generated.
UTC timestamp of when the event was raised. Auto-generated.
Computed property. Returns the class name with
"Event" stripped and uppercased (e.g. UserCreatedEvent → "USERCREATED"). Used for serialisation and event routing.Custom domain events
SubclassDomainEvent directly when you need an event that does not correspond to a standard entity lifecycle:
Typed lifecycle events
HexCore provides three generic lifecycle events that carry the triggering entity.EntityCreatedEvent[T]
The
id of the entity that was created.The full entity instance at the time of creation.
EntityUpdatedEvent[T]
The
id of the updated entity.The full entity instance after the update.
EntityDeletedEvent
The
id of the deleted entity. Does not carry entity_data because the entity is no longer available.EventBus
EventBus is the abstract port for publishing and subscribing to domain events. ServerConfig.event_bus holds the active implementation (defaults to InMemoryEventBus).
Complete example: entity → event → dispatch
The following example shows the full lifecycle: create an entity, register events, collect them from the entity, and dispatch them via the bus.In practice you never dispatch events manually like this. The Unit of Work collects entities via
collect_entity() and calls dispatch_events() automatically during commit(). See the Unit of Work guide for details.