Aggregate 1 - Declarative syntax

This example shows the Dog class used in the tutorial and module docs.

It uses the Aggregate class and the @event decorator from the library to define events that are derived from command method signatures. The bodies of the command methods are used to apply the events to the aggregate state.

The application class simply uses the aggregate class as if it were a normal Python object class.

Domain model

from typing import List

from eventsourcing.domain import Aggregate, event

class Dog(Aggregate):
    def __init__(self, name: str) -> None: = name
        self.tricks: List[str] = []

    def add_trick(self, trick: str) -> None:


from typing import Any, Dict
from uuid import UUID

from eventsourcing.application import Application
from eventsourcing.examples.aggregate1.domainmodel import Dog

class DogSchool(Application):
    is_snapshotting_enabled = True

    def register_dog(self, name: str) -> UUID:
        dog = Dog(name)

    def add_trick(self, dog_id: UUID, trick: str) -> None:
        dog: Dog = self.repository.get(dog_id)

    def get_dog(self, dog_id: UUID) -> Dict[str, Any]:
        dog: Dog = self.repository.get(dog_id)
        return {"name":, "tricks": tuple(dog.tricks)}

Test case

from unittest import TestCase

from eventsourcing.examples.aggregate1.application import DogSchool

class TestDogSchool(TestCase):
    def test_dog_school(self) -> None:
        # Construct application object.
        school = DogSchool()

        # Evolve application state.
        dog_id = school.register_dog("Fido")
        school.add_trick(dog_id, "roll over")
        school.add_trick(dog_id, "play dead")

        # Query application state.
        dog = school.get_dog(dog_id)
        assert dog["name"] == "Fido"
        assert dog["tricks"] == ("roll over", "play dead")

        # Select notifications.
        notifications =, limit=10)
        assert len(notifications) == 3

        # Take snapshot.
        school.take_snapshot(dog_id, version=3)
        dog = school.get_dog(dog_id)
        assert dog["name"] == "Fido"
        assert dog["tricks"] == ("roll over", "play dead")

        # Continue with snapshotted aggregate.
        school.add_trick(dog_id, "fetch ball")
        dog = school.get_dog(dog_id)
        assert dog["name"] == "Fido"
        assert dog["tricks"] == ("roll over", "play dead", "fetch ball")