领域模型 logo

领域模型

Organization
microwind
领域模型

业务领域的抽象表示,捕获核心业务概念、规则和行为,是DDD的核心构建块。

Overview

Publishermicrowind
Repositoryai-skills
Skill name领域模型
Stars
93
Forks
25
Bundled files
Instructions only
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.

  • Self-contained

    Everything the model needs lives in the instructions — no extra files to sync.

  • Open source

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

Installation

Install the 领域模型 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/domain-model
  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/domain-model .claude/skills/microwind-62
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable 领域模型 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 领域模型 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 领域模型 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.

领域模型 (Domain Model)

概述

领域模型是对业务领域的软件化抽象,它捕获了业务概念、规则和行为,而非数据库表结构或 UI 字段。DDD 倡导模型驱动设计(Model-Driven Design):分析模型与实现模型应当是同一个模型——业务专家口中的模型、白板上的模型、代码中的模型保持一致。

核心思想:

  • 领域模型反映业务专家的心智模型
  • 模型应该包含行为(方法),而非仅有数据(属性)
  • 领域模型独立于技术实现(数据库、框架、UI)
  • 模型与代码同步演进——一旦代码与模型脱节,模型即失去价值

为什么需要建立领域模型(8 大特点)

在通过软件实现一个业务系统时,建立一个领域模型是非常重要和必要的,因为领域模型具有以下特点:

1. 有边界的业务抽象
   领域模型是对具有某个边界的领域的抽象建模表征,
   反映了领域内业务需求的本质;只反映业务领域内所关注的部分。

2. 纯业务,与技术无关
   领域模型只反映业务,和任何技术实现无关。
   能反映实体概念(货物、书本、地址),也能反映过程概念(资金转账、人力资源)。

3. 业务逻辑的单点承载
   确保软件的业务逻辑都在一个模型中、都在一个地方。
   → 提升可维护性、业务可理解性、可重用性。

4. 平滑的知识转化
   帮助开发人员相对平滑地将领域知识转化为软件构造。

5. 贯穿软件全生命周期
   领域模型贯穿软件分析、设计、开发的整个过程。
   领域专家、设计人员、开发人员通过领域模型交流,共享知识与信息。
   → 防止需求走样,真正满足业务需求。

6. 需要反复精化
   要建立正确的领域模型并不简单,需要领域专家、设计、开发人员积极沟通共同努力,
   然后才能使大家对领域的认识不断深入,从而不断细化和完善领域模型。

7. 可视化表达
   为了让领域模型看得见,需要用一些方法来表示它。
   图是最常用的方式,但不是唯一的;代码或文字描述也能表达领域模型。

8. 软件的核心价值
   领域模型是整个软件的核心,是软件中最有价值和最具竞争力的部分。
   设计足够精良且符合业务需求的领域模型能够更快速地响应需求变化。

工程启示:

特点工程实践
① 有边界使用 限界上下文 明确边界
② 纯业务领域层不依赖数据库 / 框架(依赖倒置)
③ 单点承载用充血模型代替贫血模型,业务规则回归领域对象
④ 平滑转化代码命名直接用 通用语言
⑤ 贯穿全程需求文档、PRD、代码、测试用同一套模型和术语
⑥ 反复精化领域模型不是一次到位,定期重构
⑦ 可视化产出类图、状态机、序列图等
⑧ 核心价值核心域必须严格建模;通用域可不建

贫血模型 vs 充血模型

java
// ❌ 贫血模型:只有getter/setter,没有业务逻辑
public class Order {
    private Long id;
    private List<OrderItem> items;
    private OrderStatus status;
    private BigDecimal totalAmount;

    // 一堆 getter/setter...
}

// 业务逻辑散落在 Service 中
public class OrderService {
    public void confirmOrder(Order order) {
        if (order.getStatus() != OrderStatus.PENDING) {
            throw new IllegalStateException("只有待处理订单可以确认");
        }
        BigDecimal total = BigDecimal.ZERO;
        for (OrderItem item : order.getItems()) {
            total = total.add(item.getPrice().multiply(BigDecimal.valueOf(item.getQuantity())));
        }
        order.setTotalAmount(total);
        order.setStatus(OrderStatus.CONFIRMED);
    }
}

// ✅ 充血模型:数据和行为在一起
public class Order {
    private OrderId id;
    private List<OrderItem> items;
    private OrderStatus status;
    private Money totalAmount;

    public void confirm() {
        if (status != OrderStatus.PENDING) {
            throw new DomainException("只有待处理订单可以确认");
        }
        this.totalAmount = calculateTotal();
        this.status = OrderStatus.CONFIRMED;
    }

    public void addItem(Product product, int quantity) {
        if (status != OrderStatus.DRAFT) {
            throw new DomainException("只有草稿订单可以添加商品");
        }
        if (quantity <= 0) {
            throw new DomainException("数量必须为正数");
        }
        items.add(new OrderItem(product, quantity));
    }

    public void cancel(String reason) {
        if (!status.isCancellable()) {
            throw new DomainException("当前状态不允许取消");
        }
        this.status = OrderStatus.CANCELLED;
    }

    private Money calculateTotal() {
        return items.stream()
            .map(OrderItem::subtotal)
            .reduce(Money.ZERO, Money::add);
    }
}

Python 充血模型

python
from dataclasses import dataclass, field
from enum import Enum
from decimal import Decimal

class OrderStatus(Enum):
    DRAFT = "draft"
    PENDING = "pending"
    CONFIRMED = "confirmed"
    CANCELLED = "cancelled"

class DomainError(Exception):
    pass

@dataclass
class Order:
    id: str
    customer_id: str
    items: list = field(default_factory=list)
    status: OrderStatus = OrderStatus.DRAFT

    def add_item(self, product, quantity: int):
        if self.status != OrderStatus.DRAFT:
            raise DomainError("只有草稿订单可以添加商品")
        self.items.append(OrderItem(product, quantity))

    def submit(self):
        if not self.items:
            raise DomainError("订单不能为空")
        if self.status != OrderStatus.DRAFT:
            raise DomainError("只有草稿订单可以提交")
        self.status = OrderStatus.PENDING

    def confirm(self):
        if self.status != OrderStatus.PENDING:
            raise DomainError("只有待处理订单可以确认")
        self.status = OrderStatus.CONFIRMED

    @property
    def total(self) -> Decimal:
        return sum(item.subtotal for item in self.items)

领域模型构建步骤

1. 与领域专家沟通,理解业务流程
2. 识别核心概念(名词 → 实体/值对象)
3. 识别业务规则(动词 → 方法)
4. 确定聚合边界(一致性边界)
5. 定义领域事件(重要的业务发生)
6. 持续精化,随理解加深而演进

领域模型的特征

特征说明
表达业务语言方法名和类名反映业务术语
包含业务规则验证和约束在模型内部
技术无关不依赖数据库、HTTP、框架
自我保护不允许进入非法状态
可测试纯业务逻辑,无需外部依赖即可测试

常见误区

❌ 贫血模型 + 万能 Service——实体只有 getter/setter,业务规则在 Service 里 → 让行为回归领域对象(充血模型);Fowler 称之为反模式(Anemic Domain Model)

❌ 白板模型与代码不一致——会议讨论用一套语言,代码用另一套 → 模型驱动设计要求白板、术语表、代码同构;一旦脱节模型即失效

❌ 数据库表当模型——模型沿着 ORM 字段而非业务概念演进 → 模型从业务概念出发;持久化映射是领域模型的"投影",不是模型本身

与其他DDD概念的关系

概念关系
实体领域模型的构建块,有唯一标识
值对象领域模型的构建块,无标识
聚合一组实体和值对象的一致性边界
限界上下文领域模型的适用边界
通用语言领域模型应该反映通用语言

总结

核心:领域模型 = 数据 + 行为 + 业务规则。

实践:用充血模型替代贫血模型,让业务逻辑回归领域对象。

Frequently asked questions

What does the 领域模型 AI skill do?

业务领域的抽象表示,捕获核心业务概念、规则和行为,是DDD的核心构建块。

Why use 领域模型 on TypingMind?

Because you install it once and use it with any model. 领域模型 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 领域模型 in TypingMind?

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

Which AI models can use 领域模型?

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 领域模型?

As many as you like. As long as a model supports skills, you can use 领域模型 with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the 领域模型 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 👇