聚合 (Aggregate) logo

聚合 (Aggregate)

Organization
microwind
聚合 (Aggregate)

以聚合根为边界,包含多个相关Entity和ValueObject的集合。保证数据一致性和事务边界。

Overview

Publishermicrowind
Repositoryai-skills
Skill name聚合 (Aggregate)
Stars
93
Forks
25
Bundled files
2
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 2 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by microwind on GitHub. Read the source before you install it.

Installation

Install the 聚合 (Aggregate) AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/microwind/ai-skills/tree/main/domain-driven-design/aggregate
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/microwind/ai-skills.git /tmp/ai-skills
mkdir -p .claude/skills
cp -r /tmp/ai-skills/domain-driven-design/aggregate .claude/skills/microwind-aggregate
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable 聚合 (Aggregate) in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads 聚合 (Aggregate) on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so 聚合 (Aggregate) is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

聚合 (Aggregate)

概述

Eric Evans 将聚合定义为:一组相关对象的集合,作为数据变更的整体单元("a cluster of associated objects that we treat as a unit for the purpose of data changes")。每个聚合都有一个聚合根——作为外部访问聚合内部对象的唯一入口,以及一条由根的标识划定的一致性边界。

核心概念:

  • 聚合根(Aggregate Root):一个 Entity,作为聚合的唯一入口;外部引用只能指向它
  • 一致性边界:聚合内的不变量在事务内必须成立
  • 事务单位:原则上一个事务只修改一个聚合实例——这是 DDD 的基线规则,目的是保证聚合的独立性、可扩展性与最终一致的演进空间。仅当同一限界上下文内、跨聚合的不变量在业务上确实必须强一致(且最终一致代价高于事务耦合的代价)时,可有保留地放宽;放宽前应优先重新审视聚合边界是否划错了
  • 聚合间引用:通过 ID 引用,不通过对象引用

Java 实现

java
// 聚合根:Order
public class Order {
    private final String orderId;
    private List<OrderLine> items;  // 从属于聚合
    private OrderStatus status;
    private Money totalAmount;

    public void addItem(Product product, int quantity) {
        OrderLine line = new OrderLine(product, quantity);
        items.add(line);
        recalculateTotal();
    }

    public void cancel() {
        if (!canBeCanceled()) {
            throw new IllegalStateException("Cannot cancel order");
        }
        this.status = OrderStatus.CANCELED;
    }

    // 只通过聚合根修改数据,不直接访问items
    // ❌ 不要:order.getItems().add(...);
    // ✅ 要:order.addItem(...);

    private void recalculateTotal() {
        totalAmount = items.stream()
            .map(OrderLine::getTotal)
            .reduce(Money.ZERO, Money::add);
    }

    private boolean canBeCanceled() {
        return status == OrderStatus.PENDING || status == OrderStatus.PAID;
    }
}

// OrderLine:聚合内的对象,不应该独立存在
public class OrderLine {
    private final Product product;
    private final int quantity;

    public Money getTotal() {
        return product.getPrice().multiply(quantity);
    }
}

Python 实现

python
class Order:
    def __init__(self, order_id: str, customer_id: str):
        self.order_id = order_id
        self.customer_id = customer_id
        self.items = []
        self.status = "PENDING"
        self.total_amount = Money(0, "USD")

    def add_item(self, product: 'Product', quantity: int):
        """通过聚合根添加项"""
        line = OrderLine(product, quantity)
        self.items.append(line)
        self._recalculate_total()

    def can_be_canceled(self) -> bool:
        return self.status in ["PENDING", "PAID"]

    def cancel(self):
        if not self.can_be_canceled():
            raise ValueError("Cannot cancel order")
        self.status = "CANCELED"

    def _recalculate_total(self):
        self.total_amount = sum(
            (line.get_total() for line in self.items),
            Money(0, "USD")
        )

class OrderLine:
    def __init__(self, product: 'Product', quantity: int):
        self.product = product
        self.quantity = quantity

    def get_total(self) -> 'Money':
        return self.product.price.multiply(self.quantity)

聚合的边界设计

原则

  1. 根引用聚合内部:聚合根可以引用内部对象
  2. 外部引用聚合根:外部只能引用聚合根
  3. 内部对象不引用外部:不创建循环引用

反例

java
// ❌ 错误:从外部直接访问聚合内部
Order order = orderRepository.find("O001");
OrderLine line = order.getItems().get(0);  // 直接访问!
line.setQuantity(100);  // 修改!

// ✅ 正确:通过聚合根
Order order = orderRepository.find("O001");
order.updateItem(0, 100);  // 通过聚合根修改

最佳实践

  1. 设定清晰的聚合边界
  2. 只通过聚合根操作
  3. 保护聚合内的一致性
  4. 聚合根是仓储的访问点

Vaughn Vernon 的四条聚合设计规则

Vaughn Vernon 在 Implementing Domain-Driven Design(IDDD,红皮书)中提炼了四条被广泛认可的聚合设计经验:

  1. 在聚合边界内保护真正的业务不变量 ——只有"必须强一致"的规则,才值得放进同一个聚合。
  2. 设计小聚合 ——优先单根 + 少量值对象;避免把所有相关 Entity 全塞进一个根。
  3. 聚合之间通过全局唯一标识引用 ——不要用对象引用穿透聚合边界。
  4. 聚合外部使用最终一致性 ——跨聚合的协同通过领域事件与最终一致机制完成。
java
// ✅ 遵循规则:小聚合 + ID 引用
public class Order {
    private OrderId id;
    private CustomerId customerId;        // 通过 ID 引用其他聚合,而非 Customer 对象
    private List<OrderLine> items;        // 聚合内部实体
    private OrderStatus status;
}

// ❌ 违反规则:穿透边界、对象引用
public class Order {
    private Customer customer;            // 直接持有另一聚合的对象引用
    private List<OrderLine> items;
    public void changeCustomerEmail(String email) {
        customer.setEmail(email);         // 跨聚合直接写——破坏单写规则
    }
}

聚合大小如何把握

"小聚合"是原则,但"多小才算小"没有标准答案。下列实操判据可帮助判断:

判断聚合是否过大:

  • 一次加载需要拉取大量子对象(数十乃至数百条),多数操作只用到其中一小部分
  • 频繁出现"我只想改这一个字段,却要先 load 整个聚合"的吐槽
  • 高并发下因聚合粒度大而触发版本冲突 / 乐观锁失败
  • 同一聚合内的不同部分修改频率明显不同——通常意味着隐藏着另一个聚合

判断聚合是否过小(被切碎了):

  • 一个业务操作需要同时改 3 个以上聚合并维护强一致
  • 频繁需要跨聚合 join 才能完成基本查询
  • 业务规则被迫散落到应用服务里"做编排式校验"

经验判据:

  • 优先把真正共享一致性的最小子集放进同一聚合
  • 当聚合内某子集修改频率独立、查询路径独立,就考虑独立成新聚合
  • "先粗后细"——不确定时先做大聚合,等出现性能或冲突信号再拆,比反过来从碎片合并容易得多

常见误区

❌ 上帝聚合——把 Order + Customer + Product + Inventory 全塞进一个根 → 用 ID 引用其他聚合,每个聚合只承载自己的真实不变量

❌ 从外部直接操作聚合内部对象——order.getItems().add(...) 跳过聚合根校验 → 暴露行为方法 order.addItem(...),集合修改入口只在聚合根内部

❌ 跨聚合写在一个事务里——一次事务同时更新 Order、Inventory、Payment → 同一事务只改一个聚合,跨聚合协同用领域事件 + 最终一致性

聚合是建立清晰边界、保证数据一致的关键设计。

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the 聚合 (Aggregate) AI skill do?

以聚合根为边界,包含多个相关Entity和ValueObject的集合。保证数据一致性和事务边界。

Why use 聚合 (Aggregate) on TypingMind?

Because you install it once and use it with any model. 聚合 (Aggregate) is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install 聚合 (Aggregate) in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/microwind/ai-skills/tree/main/domain-driven-design/aggregate. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use 聚合 (Aggregate)?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with 聚合 (Aggregate)?

As many as you like. As long as a model supports skills, you can use 聚合 (Aggregate) with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the 聚合 (Aggregate) AI skill free?

It is published on GitHub by microwind. Check the repository for licensing terms. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇