Skip to content
Back to skills

Aggregate

ASecurity

Define a Protean aggregate, the root entity of a cluster of domain objects treated as a single unit for data changes. Aggregates are fundamental building blocks that encapsulate business logic, enforce invariants, and define transaction boundaries. Use when creating domain entities with identity that change over time, when modeling business concepts that need to enforce consistency rules, when the user asks to "create an aggregate", "add a domain model", "define a root entity", or when they d...

  • 46 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added September 20, 2026
documentationpythongodatabasedocumentation

Security analysis

A100/100

Pro scans all 13 files and shows the line behind each finding

Scanned December 3, 2026

npx -y skills add proteanhq/protean --skill aggregate --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Aggregate?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Aggregate
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/proteanhq-aggregate/badge)](https://www.skillsdirectory.com/skills/proteanhq-aggregate)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: aggregate
description: Define a Protean aggregate, the root entity of a cluster of domain objects treated as a single unit for data changes. Aggregates are fundamental building blocks that encapsulate business logic, enforce invariants, and define transaction boundaries. Use when creating domain entities with identity that change over time, when modeling business concepts that need to enforce consistency rules, when the user asks to "create an aggregate", "add a domain model", "define a root entity", or when they describe a core business concept that needs to persist state and enforce rules (like Order, Customer, Account, Product). Aggregates can contain entities and value objects, and are responsible for maintaining business invariants across their object graph.
license: Apache-2.0
compatibility: Requires Python 3.11+, protean framework
metadata:
  author: proteanhq
  version: "0.1"
  category: element
  diagnostic_codes:
    - AGGREGATE_NOT_NOUN
    - AGGREGATE_NO_INVARIANTS
    - AGGREGATE_TOO_LARGE
    - AGGREGATE_WITHOUT_COMMAND_HANDLER
    - CROSS_AGGREGATE_REFERENCE
---

# Aggregate

An aggregate is the unit a repository loads and saves, and the place a domain's business rules are enforced. This skill defines one: its fields, associations, invariants and options.

## Basic structure

An aggregate is defined using the `@domain.aggregate` decorator:

```python
from protean import Domain
from protean.fields import String, Date, Integer

domain = Domain()

@domain.aggregate
class Post:
    title: String(required=True, max_length=200)
    content: String(max_length=5000)
    status: String(max_length=20, default="draft")
    published_on: Date()
    view_count: Integer(default=0)
```

## Key rules

1. **Aggregates are the root of an object graph**: They manage entities and value objects within their boundary
2. **Aggregates enforce business invariants**: Codify every business rule as an `@invariant.pre` or `@invariant.post` method, one rule per invariant
3. **Aggregates are transaction boundaries**: All changes within an aggregate are saved together
4. **Aggregates have automatic identity**: An `id` field is auto-generated unless `auto_add_id_field=False`
5. **Aggregates are versioned**: Every aggregate has a `_version` field for optimistic concurrency control
6. **Keep aggregates small**: `check` flags an aggregate with more entity types than the configured `aggregate_size_limit` (default 5) as `AGGREGATE_TOO_LARGE`. Split a growing aggregate, or raise the limit if the size is intentional
7. **Entities accessed only through aggregates**: Never reference entities directly from outside
8. **Each aggregate is independent**: Don't load other aggregates within aggregate methods

## Fields and associations

### Simple Fields with built-in validations

**Important**: Protean fields come with built-in validation parameters. Always use these instead of manual validation in methods.

```python
from datetime import datetime, timezone

from protean.fields import String, Integer, Float, Decimal, Boolean, Date, DateTime, Text


def utc_now():
    return datetime.now(timezone.utc)


@domain.aggregate
class Product:
    name: String(required=True, max_length=200, min_length=3)
    description: Text()
    price: Decimal(precision=19, scale=4, required=True, min_value=0.01)  # Field-level validation
    in_stock: Boolean(default=True)
    stock_count: Integer(default=0, min_value=0)  # Field-level validation
    discount_percent: Float(min_value=0.0, max_value=100.0, default=0.0)
    created_at: DateTime(default=utc_now)
```

**Common field validation parameters**:
- **All fields**: `required`, `default`, `unique`, `choices`, `validators`
- **String/Text**: `max_length`, `min_length`, `sanitize`
- **Integer/Float/Decimal**: `min_value`, `max_value`
- **Decimal**: `precision`, `scale` (use `Decimal(precision=19, scale=4)` for money)

**Use field validations for**:
- Data type constraints (string length, number ranges)
- Required/optional status
- Uniqueness constraints
- Default values

**Use invariants for**:
- Cross-field business rules (e.g., "end_date must be after start_date")
- Complex domain logic involving multiple attributes
- State-dependent validations

### HasOne Association

One-to-one relationship with an entity:

```python
from protean.fields import HasOne

@domain.aggregate
class Order:
    customer_id: Identifier(required=True)
    shipping_info = HasOne("ShippingInfo")

@domain.entity(part_of="Order")
class ShippingInfo:
    address: String(required=True, max_length=500)
    city: String(required=True, max_length=100)
```

**Note**: For `HasOne` relationships, access the related entity directly through the field (e.g., `order.shipping_info`). No special helper methods are needed.

### HasMany Association

One-to-many relationship with entities:

```python
from protean.fields import HasMany

@domain.aggregate
class Order:
    customer_id: Identifier(required=True)
    line_items = HasMany("LineItem")

@domain.entity(part_of="Order")
class LineItem:
    product_id: Identifier(required=True)
    quantity: Integer(required=True)
    unit_price: Decimal(precision=19, scale=4, required=True)
```

**Auto-generated helper methods**: When you define a `HasMany` field, Protean automatically creates helper methods for managing the association. For the `line_items` field above, these methods are generated:

- `add_line_items(item)`: Add one or more items to the collection
- `remove_line_items(item)`: Remove an item from the collection
- `get_one_from_line_items(id=item_id)`: Get the one item whose fields match the keyword arguments. Raises `ObjectNotFoundError` (from `protean.exceptions`) when nothing matches, and `TooManyObjectsError` when more than one item matches
- `filter_line_items(**criteria)`: Return the items whose fields equal the given values, for example `order.filter_line_items(product_id="P1")`. It matches equality only; there are no `__gt`-style operators

Both lookup helpers take keyword arguments. A positional argument raises `TypeError`.

Protean gives each entity a reference to its aggregate: `item.order` and `item.order_id` work with no `Reference` field declared.

**Important**: Do NOT manually create these methods: they are automatically available. Only create custom methods if you need behavior different from the defaults (e.g., custom validation when adding items).

### ValueObject Field

For complex immutable data types:

```python
from protean.fields import ValueObject

@domain.value_object
class Money:
    amount: Decimal(precision=19, scale=4, required=True)
    currency: String(max_length=3, default="USD")

@domain.aggregate
class Order:
    customer_id: Identifier(required=True)
    line_items = HasMany("LineItem")
    total = ValueObject(Money)
```

## Initialization

Create aggregate instances by passing field values as keyword arguments. Instances need an initialized domain and an active domain context:

```python
domain.init(traverse=False)

with domain.domain_context():
    # Simple aggregate
    post = Post(
        title="Getting Started with Protean",
        content="Protean is a DDD framework...",
        status="draft"
    )

    # With entities (using auto-generated add_line_items() helper)
    order = Order(customer_id="C123")
    order.add_line_items(LineItem(product_id="P1", quantity=2, unit_price="50.00"))

    # With value objects
    order = Order(
        customer_id="C123",
        total=Money(amount="100.00", currency="USD")
    )
```

## State mutations

Define methods on the aggregate to change state:

```python
@domain.aggregate
class Order:
    customer_id: Identifier(required=True)
    status: String(max_length=20, default="draft")
    line_items = HasMany("LineItem")

    def add_item(self, product_id: str, quantity: int, price):
        """Add an item to the order with custom business logic.

        Note: This uses the auto-generated add_line_items() helper.
        Create custom methods like this when you need additional validation,
        calculations, or business logic beyond simple item addition.
        """
        item = LineItem(product_id=product_id, quantity=quantity, unit_price=price)
        self.add_line_items(item)  # Uses auto-generated helper

    def place_order(self):
        """Place the order."""
        if not self.line_items:
            raise ValueError("Cannot place empty order")
        self.status = "placed"

    def cancel(self):
        """Cancel the order."""
        if self.status == "shipped":
            raise ValueError("Cannot cancel shipped order")
        self.status = "cancelled"


@domain.entity(part_of=Order)
class LineItem:
    product_id: Identifier(required=True)
    quantity: Integer(required=True)
    unit_price: Decimal(precision=19, scale=4, required=True)
```

## Invariants (business rules)

Protean checks post-invariants at the end of `__init__`. Every field assignment runs the pre-invariants before the new value is set and the post-invariants after it. Adding, removing or changing a child entity checks the root's invariants too. To change several fields that are only valid together, wrap the changes in `with atomic_change(order):` (`from protean import atomic_change`), which runs the pre-invariants once at the start and the post-invariants once at the end of the block. `repository.add()` does not check invariants again. When an invariant fails on a field assignment, the field keeps its old value.

Use `@invariant.pre` (checked before changes) and `@invariant.post` (checked after changes):

- **`@invariant.post`**: Validates after changes occur. Use for constraints that depend on the final state (e.g., "balance must not be negative"). Checked after initialization and after attribute updates.
- **`@invariant.pre`**: Validates before changes occur. Use for preconditions that must be true before allowing state changes (e.g., "account must be active"). **Note**: Pre invariants are NOT checked during initialization.

An invariant raises `ValidationError` with a dict that maps the field at fault to a list of messages: `ValidationError({"status": ["..."]})`. A rule that compares fields, such as a balance against an overdraft limit, uses the key `_entity`. Protean catches only `ValidationError` from an invariant and attaches the code `INVARIANT_PRE_FAILED` or `INVARIANT_POST_FAILED` (a `code=` argument on the decorator replaces it). A `ValueError` or a custom exception gets no code, and a plain-string `ValidationError("...")` fails with a `TypeError`.

```python
from protean import invariant
from protean.exceptions import ValidationError

@domain.aggregate
class Account:
    balance: Decimal(precision=19, scale=4, default=0)
    overdraft_limit: Decimal(precision=19, scale=4, default=0)
    status: String(default="active")

    # Post invariants - checked after initialization and after changes
    @invariant.post
    def balance_must_be_above_overdraft_limit(self):
        """Balance cannot fall below the negative overdraft limit.

        Granular rule: Each invariant checks ONE business constraint.
        """
        if self.balance < -self.overdraft_limit:
            raise ValidationError(
                {"_entity": ["Balance cannot be below overdraft limit"]}
            )

    # Pre invariants - only checked before changes (NOT during initialization)
    @invariant.pre
    def account_must_be_active(self):
        """Account must be active for transactions.

        Pre-check ensures we don't attempt invalid operations.
        """
        if self.status != "active":
            raise ValidationError({"status": ["Account is not active"]})

    def withdraw(self, amount):
        """Withdraw money from account.

        Note: Parameter validations (amount > 0) belong in the method
        when they validate method arguments, not aggregate fields.
        Field-level constraints should be defined on the field itself.
        """
        if amount <= 0:
            raise ValueError("Withdrawal amount must be positive")
        self.balance -= amount  # Invariants automatically checked after this
```

**Best practices**:
1. **Use descriptive names**: Name should clearly describe the rule being enforced
2. **Prefer `@invariant.post`**: Use for most validations that check final state
3. **Use `@invariant.pre` sparingly**: Only for preconditions that must prevent state changes

## Inheritance

Create abstract base aggregates for shared fields:

```python
from datetime import datetime, timezone

def utc_now():
    return datetime.now(timezone.utc)

@domain.aggregate(abstract=True)
class TimeStamped:
    """Abstract base with timestamp fields."""
    created_at: DateTime(default=utc_now)
    updated_at: DateTime(default=utc_now)

@domain.aggregate
class User(TimeStamped):
    """Concrete aggregate inheriting timestamps."""
    email: String(required=True, max_length=255)
    name: String(required=True, max_length=200)
```

An abstract base gives each subclass an `id` field. When the subclasses declare their own identifier field, pass `auto_add_id_field=False` to the base so that they get no extra `id`.

## Configuration options

Pass options to the decorator:

```python
@domain.aggregate(
    auto_add_id_field=False,    # Control automatic ID field
    database="default",         # Database name from the domain config's `databases` section
    schema_name="customer_orders",  # Table/collection name
    stream_category="order"     # Event sourcing stream category
)
class ArchivedOrder:
    order_number: String(identifier=True, max_length=20)
```

`abstract=True` marks a base aggregate that cannot be instantiated (see [Inheritance](#inheritance)). An inner `class Meta:` on the aggregate is ignored; pass every option to the decorator.

See [Configuration Reference](references/configuration.md) for detailed documentation.

## Persistence

Aggregates are persisted using repositories:

```python
from protean import current_domain

domain.init(traverse=False)

with domain.domain_context():
    repo = current_domain.repository_for(Order)

    # Create
    order = Order(customer_id="C123")
    order.add_item("P1", 2, "50.00")  # Custom method using add_line_items()
    repo.add(order)

    # Retrieve
    order = repo.get(order.id)

    # Update
    order.place_order()
    repo.add(order)

    # "Delete": record the change as a status, then persist it with add()
    order.cancel()
    repo.add(order)
```

Repositories have no `save()` or `remove()` method. `add()` both inserts and updates. Model removal as a state change, such as `cancel()` above.

## Quick example

```python
from protean import Domain, invariant
from protean.exceptions import ValidationError
from protean.fields import Decimal, HasMany, Identifier, Integer, String

from decimal import Decimal as D

domain = Domain()

@domain.entity(part_of="Order")
class LineItem:
    product_id: String(required=True, max_length=50)
    quantity: Integer(required=True, min_value=1)
    unit_price: Decimal(precision=19, scale=4, required=True)

    @property
    def subtotal(self) -> D:
        return self.quantity * self.unit_price

@domain.aggregate
class Order:
    customer_id: Identifier(required=True)
    status: String(max_length=20, default="draft")
    line_items = HasMany(LineItem)

    @property
    def total(self) -> D:
        return sum((item.subtotal for item in self.line_items), D("0"))

    @invariant.post
    def placed_order_must_have_items(self):
        if self.status == "placed" and not self.line_items:
            raise ValidationError({"line_items": ["Cannot place empty order"]})

    def place_order(self):
        if not self.line_items:
            raise ValueError("Cannot place empty order")
        self.status = "placed"

# Usage
domain.init(traverse=False)

with domain.domain_context():
    order = Order(customer_id="CUST-12345")
    # Using auto-generated add_line_items() helper
    order.add_line_items(LineItem(product_id="PROD-001", quantity=2, unit_price="29.99"))
    order.add_line_items(LineItem(product_id="PROD-002", quantity=1, unit_price="49.99"))
    order.place_order()

    print(f"Order total: ${order.total:.2f}")
    print(f"Status: {order.status}")
```

## Common mistakes

### ❌ Anemic aggregates (only data, no behavior)

```python
@domain.aggregate
class Order:
    status: String(default="draft")
    # No methods! Business logic scattered elsewhere
```

✅ **Instead**: Encapsulate behavior in aggregate methods.

See [Anti-patterns](references/anti-patterns.md) for additional mistakes: oversized aggregates, direct entity access, transaction boundary violations, manually recreating auto-generated helpers.

### What `check` reports

`check` inspects your aggregates and reports these diagnostics:

- `AGGREGATE_NO_INVARIANTS`: the aggregate declares no `@invariant.pre` or `@invariant.post` method, so it enforces no business rules and is usually an anemic data holder. Add the invariants it must always satisfy, or reconsider whether this concept is an aggregate.
- `AGGREGATE_NOT_NOUN`: the aggregate's name ends in a suffix that reads as a verb, gerund, or adjective (`OrderProcessing`, `Cancelable`). Rename it to the domain-concept noun it models (`Order`).
- `AGGREGATE_TOO_LARGE`: the aggregate declares more entity types than the configured `[lint] aggregate_size_limit`. The count is of entity classes in the cluster, not of rows loaded at runtime. Split it into smaller aggregates, or raise the limit if the size is intentional.
- `AGGREGATE_WITHOUT_COMMAND_HANDLER`: the aggregate has no command handler, so nothing can change its state. Add a command handler for it, or model it as a read-only projection if no writes are expected.
- `CROSS_AGGREGATE_REFERENCE`: a field holds a direct `Reference` to another aggregate root. Hold the other aggregate by its identifier instead (`<other>_id: Identifier()`) and load it through its own repository when needed.

## Detailed references

### Core Concepts
- [Aggregates with Entities](references/with-entities.md): HasOne and HasMany relationships
- [Aggregates with Value Objects](references/with-value-objects.md): Using value objects for complex data
- [Invariants](references/invariants.md): Enforcing business rules with pre/post conditions
- [Configuration Options](references/configuration.md): Complete reference for all options
- [Anti-patterns](references/anti-patterns.md): Common mistakes and how to avoid them

### Complete Examples
- [Simple Aggregate](assets/aggregate_simple.py): Basic aggregate with fields
- [Aggregate with Entities](assets/aggregate_with_entity.py): HasOne and HasMany relationships
- [Aggregate with Value Objects](assets/aggregate_with_value_object.py): Money, Address examples
- [Aggregate with Invariants](assets/aggregate_with_invariants.py): Business rule enforcement
- [Aggregate Inheritance](assets/aggregate_inheritance.py): Abstract bases and inheritance

### Related Skills
- `entity`: Entities within aggregates
- `value-object`: Value objects for complex data
- `command`: Commands that trigger aggregate state changes
- `event`: Events raised by aggregates
- `repository`: Persisting and retrieving aggregates
- `event-sourced-aggregate`: Event-sourced aggregates with @apply, from_events(), fact events

## Verify your work

- [Verify with check](../../references/verify-with-check.md): run `check` and resolve what it reports

Files in this skill

  • SKILL.md15 KB
  • __init__.py45 B
  • assets/__init__.py41 B
  • assets/aggregate_inheritance.py4.9 KB
  • assets/aggregate_simple.py1.4 KB
  • assets/aggregate_with_entity.py3.5 KB
  • assets/aggregate_with_invariants.py6.9 KB
  • assets/aggregate_with_value_object.py4.3 KB
  • references/anti-patterns.md13 KB
  • references/configuration.md8.3 KB
  • references/invariants.md9.1 KB
  • references/with-entities.md5.5 KB
  • references/with-value-objects.md7 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in documentation →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.