Logo di Domain Modeling

Domain Modeling

Community
zhaoxuya520
domain-modeling

设计业务领域模型 / 实体 / 值对象 / 聚合 / 业务规则时使用。覆盖所有主流后端语言(Java / TS / Python / Go / Ruby / C#)。融合 DDD(Eric Evans)+ 充血模型 + 状态机模式。

Panoramica

Pubblicato dazhaoxuya520
RepositoryAI-Fullstack-Delivery-Workflow
Nome della skilldomain-modeling
Stelle
56
Fork
18
File inclusi
1
Link
  • Istruzioni in Markdown

    Un file SKILL.md che il modello carica quando serve, consumando token solo quando una richiesta è effettivamente pertinente.

  • Funziona con qualsiasi LLM

    Le skill IA sono in semplice Markdown, non in codice specifico di un fornitore, quindi funzionano con GPT, Claude, Gemini, Grok o un modello locale.

  • File inclusi: 1

    Script, template e riferimenti che il modello può leggere mentre lavora. I file sono di sola lettura e non vengono mai eseguiti.

  • Open source

    Pubblicata da zhaoxuya520 su GitHub. Leggi i sorgenti prima di installarla.

Installazione

Installa la skill IA Domain Modeling in TypingMind per usarla con qualsiasi LLM, oppure copiala in un altro agente in grado di leggere SKILL.md.

1

Installa in TypingMind

TypingMind installa una skill direttamente dalla sua cartella GitHub: legge SKILL.md, raccoglie i file delle risorse e salva il risultato in locale.

  1. Apri l'app e vai su Plugin → Skill.
  2. Scegli "Installa da GitHub".
  3. Incolla l'URL della cartella della skill riportato qui sotto e conferma.
    https://github.com/zhaoxuya520/AI-Fullstack-Delivery-Workflow/tree/master/workflows/backend-engineer/skills/domain-modeling
  4. Attiva la skill in ogni chat in cui vuoi averla a disposizione.
Plugin → Skill → Aggiungi skill → Da URL GitHub, poi incolla l'URL della cartella e premi Continua.
2

Installa in un altro agente

Qualsiasi agente che legge il formato Agent Skills può usare questa skill: copia la cartella della skill nella directory delle skill di quell'agente.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/zhaoxuya520/AI-Fullstack-Delivery-Workflow.git /tmp/AI-Fullstack-Delivery-Workflow
mkdir -p .claude/skills
cp -r /tmp/AI-Fullstack-Delivery-Workflow/workflows/backend-engineer/skills/domain-modeling .claude/skills/domain-modeling
Dopo la copia, riavvia Claude Code per caricare la nuova skill.

Usala in TypingMind

Attiva Domain Modeling in qualsiasi chat di TypingMind e al resto pensa il modello. Il nome e la descrizione della skill sono nel prompt di sistema e, non appena arriva una richiesta pertinente, il modello carica da solo le istruzioni complete: non devi mai richiamarla manualmente e non consuma token finché non viene effettivamente usata.

Il modello carica automaticamente Domain Modeling non appena riceve una richiesta pertinente.

Funziona con qualsiasi modello IA

Le skill IA sono semplici istruzioni in Markdown, non codice specifico di un fornitore, quindi Domain Modeling non è vincolata al modello per cui è stata scritta. Installala una sola volta in TypingMind e usala con GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama o un modello locale che esegui tu, sempre con le tue chiavi API.

  • Caricata solo quando serve

    Il prompt di sistema contiene solo il nome e la descrizione. Le istruzioni vengono recuperate alla prima richiesta pertinente, quindi una skill inutilizzata non costa nulla.

  • Cambia modello durante la chat

    Poiché la skill è composta da istruzioni e non da codice, continua a funzionare anche se cambi modello: il nuovo modello legge lo stesso file SKILL.md.

Istruzioni della skill

Questo è il contenuto di SKILL.md che il modello carica. Leggilo prima dell'installazione: una skill è un insieme di istruzioni che il tuo modello seguirà.

领域建模(Domain Modeling)

参考来源:Eric Evans《Domain-Driven Design》、Vaughn Vernon《Implementing DDD》、Martin Fowler 充血模型 / 贫血模型、Robert Martin Clean Architecture。

适用场景

  • 复杂业务规则需要清晰建模
  • 状态流转 / 工作流(订单 / 审批 / 支付)
  • 多种业务方式涉及同一对象
  • 防止业务逻辑散落到 Service / Controller
  • 重构贫血模型为充血模型

核心原则

text
1. 业务规则放进 Domain Model
   不放 Controller,不放 Service,不放 Repository

2. 实体 vs 值对象
   实体(Entity):有唯一身份(ID),可变
   值对象(Value Object):无身份,不可变,靠属性比较

3. 聚合根(Aggregate Root)
   外部只能通过聚合根访问聚合内的实体
   一个事务只修改一个聚合

4. 业务不变量(Invariants)
   保证对象在任何时候都满足业务规则
   构造函数 / 工厂方法强制校验

5. 命令式 → 业务式 命名
   ❌ user.setStatus("active")
   ✅ user.activate()

6. 状态流转用状态机
   用方法表达状态转换
   非法转换抛异常

7. 不依赖框架
   Domain 层应该能独立编译运行

充血模型 vs 贫血模型

贫血模型(反模式,常见)

java
// Order 只是数据容器
public class Order {
  private Long id;
  private String status;
  private BigDecimal totalAmount;
  // 全是 getter / setter
}

// 业务逻辑全在 Service
public class OrderService {
  public void cancelOrder(Long orderId) {
    Order order = orderRepository.findById(orderId).orElseThrow();
    if (!order.getStatus().equals("paid")) {
      throw new IllegalStateException("...");
    }
    if (order.getCreatedAt().isBefore(now().minusDays(7))) {
      throw new IllegalStateException("...");
    }
    order.setStatus("cancelled");
    order.setCancelledAt(now());
    orderRepository.save(order);
  }
}

问题:

  • 业务规则分散
  • 同一规则在多处实现
  • 容易绕过(order.setStatus("cancelled") 直接调)

充血模型(推荐)

java
public class Order {
  private Long id;
  private OrderStatus status;
  private BigDecimal totalAmount;
  private LocalDateTime createdAt;
  private LocalDateTime cancelledAt;
  
  // 私有构造,强制使用工厂方法
  private Order() {}
  
  public static Order create(User user, List<OrderItem> items) {
    if (items.isEmpty()) {
      throw new IllegalArgumentException("Order must have at least one item");
    }
    Order order = new Order();
    order.status = OrderStatus.DRAFT;
    order.totalAmount = items.stream()
        .map(OrderItem::getSubtotal)
        .reduce(BigDecimal.ZERO, BigDecimal::add);
    order.createdAt = LocalDateTime.now();
    return order;
  }
  
  // 业务方法(不是 setStatus)
  public void cancel() {
    if (this.status != OrderStatus.PAID) {
      throw new IllegalStateException("Only paid orders can be cancelled");
    }
    if (this.createdAt.isBefore(LocalDateTime.now().minusDays(7))) {
      throw new IllegalStateException("Cancellation window has expired");
    }
    this.status = OrderStatus.CANCELLED;
    this.cancelledAt = LocalDateTime.now();
  }
  
  public void pay() {
    if (this.status != OrderStatus.SUBMITTED) {
      throw new IllegalStateException("Only submitted orders can be paid");
    }
    this.status = OrderStatus.PAID;
  }
  
  // 没有 setStatus(),无法绕过业务规则
}
java
// Service 变薄,编排为主
public class OrderService {
  public void cancelOrder(Long orderId) {
    Order order = orderRepository.findById(orderId).orElseThrow();
    order.cancel();  // 业务规则由 Order 自己保证
    orderRepository.save(order);
  }
}

实体 vs 值对象

实体(Entity)

java
public class User {
  private Long id;          // 唯一身份
  private String email;
  private String name;
  // ...
  
  // 相等性靠 ID
  @Override
  public boolean equals(Object o) {
    if (!(o instanceof User other)) return false;
    return id != null && id.equals(other.id);
  }
}

值对象(Value Object)

java
// 不可变 + 靠属性比较
public record Money(BigDecimal amount, String currency) {
  public Money {
    if (amount.signum() < 0) {
      throw new IllegalArgumentException("Amount cannot be negative");
    }
    if (currency == null || currency.length() != 3) {
      throw new IllegalArgumentException("Currency must be ISO 4217");
    }
  }
  
  public Money add(Money other) {
    if (!this.currency.equals(other.currency)) {
      throw new IllegalStateException("Currency mismatch");
    }
    return new Money(this.amount.add(other.amount), this.currency);
  }
}

// 用法
Money price = new Money(new BigDecimal("99.99"), "USD");
Money total = price.add(shipping);  // 返回新对象,原对象不变

何时用什么

text
有唯一身份(用户、订单、产品) → 实体
描述属性(金额、地址、日期范围、坐标) → 值对象
身份不可变(一旦创建就不变) → 值对象
状态会变化(订单状态、用户状态) → 实体

聚合(Aggregate)

java
// 订单聚合:Order 是聚合根,OrderItem 在聚合内
public class Order {
  private Long id;
  private List<OrderItem> items = new ArrayList<>();
  
  public void addItem(Product product, int quantity) {
    if (this.status != OrderStatus.DRAFT) {
      throw new IllegalStateException("Cannot modify non-draft order");
    }
    this.items.add(new OrderItem(product, quantity));
    recalculateTotal();
  }
  
  public void removeItem(Long itemId) {
    if (this.status != OrderStatus.DRAFT) {
      throw new IllegalStateException("Cannot modify non-draft order");
    }
    this.items.removeIf(i -> i.getId().equals(itemId));
    recalculateTotal();
  }
  
  // OrderItem 不能被外部直接 new 或修改
  // 必须通过 Order 的方法
}

聚合规则:

  • 外部代码持有聚合根的引用,不直接持有内部实体
  • 一个事务只修改一个聚合
  • 聚合之间用 ID 关联,不直接持有引用

状态机模式

java
public enum OrderStatus {
  DRAFT,
  SUBMITTED,
  PAID,
  SHIPPED,
  DELIVERED,
  CANCELLED,
  REFUNDED;
}

public class Order {
  // 状态转换规则集中在一处
  private static final Map<OrderStatus, Set<OrderStatus>> ALLOWED_TRANSITIONS = Map.of(
    OrderStatus.DRAFT, Set.of(OrderStatus.SUBMITTED, OrderStatus.CANCELLED),
    OrderStatus.SUBMITTED, Set.of(OrderStatus.PAID, OrderStatus.CANCELLED),
    OrderStatus.PAID, Set.of(OrderStatus.SHIPPED, OrderStatus.REFUNDED),
    OrderStatus.SHIPPED, Set.of(OrderStatus.DELIVERED),
    OrderStatus.DELIVERED, Set.of(OrderStatus.REFUNDED)
    // CANCELLED / REFUNDED 是终态
  );
  
  private void transitionTo(OrderStatus newStatus) {
    Set<OrderStatus> allowed = ALLOWED_TRANSITIONS.getOrDefault(this.status, Set.of());
    if (!allowed.contains(newStatus)) {
      throw new IllegalStateException(
        String.format("Cannot transition from %s to %s", this.status, newStatus)
      );
    }
    this.status = newStatus;
  }
  
  public void submit() { transitionTo(OrderStatus.SUBMITTED); }
  public void pay() { transitionTo(OrderStatus.PAID); }
  public void ship() { transitionTo(OrderStatus.SHIPPED); }
  public void deliver() { transitionTo(OrderStatus.DELIVERED); }
}

TypeScript 范式

typescript
// Value Object
export class Money {
  constructor(
    public readonly amount: number,
    public readonly currency: string,
  ) {
    if (amount < 0) throw new Error('Amount cannot be negative');
    if (currency.length !== 3) throw new Error('Currency must be ISO 4217');
  }
  
  add(other: Money): Money {
    if (this.currency !== other.currency) throw new Error('Currency mismatch');
    return new Money(this.amount + other.amount, this.currency);
  }
  
  equals(other: Money): boolean {
    return this.amount === other.amount && this.currency === other.currency;
  }
}

// Entity
export class Order {
  private constructor(
    public readonly id: number,
    private _status: OrderStatus,
    private _items: OrderItem[],
    private _total: Money,
  ) {}
  
  static create(items: OrderItem[]): Order {
    if (items.length === 0) throw new Error('Order must have items');
    const total = items.reduce(
      (sum, item) => sum.add(item.subtotal),
      new Money(0, items[0].currency),
    );
    return new Order(0, OrderStatus.DRAFT, items, total);
  }
  
  get status(): OrderStatus { return this._status; }
  
  cancel(): void {
    if (this._status !== OrderStatus.PAID) {
      throw new Error('Only paid orders can be cancelled');
    }
    this._status = OrderStatus.CANCELLED;
  }
}

Python 范式

python
from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum

class OrderStatus(Enum):
    DRAFT = 'DRAFT'
    SUBMITTED = 'SUBMITTED'
    PAID = 'PAID'
    CANCELLED = 'CANCELLED'

# Value Object
@dataclass(frozen=True)
class Money:
    amount: Decimal
    currency: str
    
    def __post_init__(self):
        if self.amount < 0:
            raise ValueError('Amount cannot be negative')
        if len(self.currency) != 3:
            raise ValueError('Currency must be ISO 4217')
    
    def add(self, other: 'Money') -> 'Money':
        if self.currency != other.currency:
            raise ValueError('Currency mismatch')
        return Money(self.amount + other.amount, self.currency)

# Entity
@dataclass
class Order:
    id: int
    status: OrderStatus = OrderStatus.DRAFT
    items: list = field(default_factory=list)
    total: Money = None
    created_at: datetime = field(default_factory=datetime.utcnow)
    
    @classmethod
    def create(cls, items):
        if not items:
            raise ValueError('Order must have items')
        return cls(id=0, items=items, total=sum_items(items))
    
    def cancel(self):
        if self.status != OrderStatus.PAID:
            raise ValueError('Only paid orders can be cancelled')
        self.status = OrderStatus.CANCELLED

Go 范式

go
package model

type OrderStatus string

const (
    OrderStatusDraft     OrderStatus = "DRAFT"
    OrderStatusSubmitted OrderStatus = "SUBMITTED"
    OrderStatusPaid      OrderStatus = "PAID"
    OrderStatusCancelled OrderStatus = "CANCELLED"
)

// Value Object
type Money struct {
    Amount   int64  // 分
    Currency string
}

func NewMoney(amount int64, currency string) (Money, error) {
    if amount < 0 {
        return Money{}, errors.New("amount cannot be negative")
    }
    if len(currency) != 3 {
        return Money{}, errors.New("currency must be ISO 4217")
    }
    return Money{Amount: amount, Currency: currency}, nil
}

func (m Money) Add(other Money) (Money, error) {
    if m.Currency != other.Currency {
        return Money{}, errors.New("currency mismatch")
    }
    return Money{Amount: m.Amount + other.Amount, Currency: m.Currency}, nil
}

// Entity
type Order struct {
    id     int64
    status OrderStatus
    items  []OrderItem
    total  Money
}

func NewOrder(items []OrderItem) (*Order, error) {
    if len(items) == 0 {
        return nil, errors.New("order must have items")
    }
    return &Order{
        status: OrderStatusDraft,
        items:  items,
    }, nil
}

func (o *Order) Cancel() error {
    if o.status != OrderStatusPaid {
        return errors.New("only paid orders can be cancelled")
    }
    o.status = OrderStatusCancelled
    return nil
}

工作流程

text
1. 识别业务实体(与 PM 沟通)
   ↓
2. 区分实体 vs 值对象
   ↓
3. 识别聚合(事务边界)
   ↓
4. 列业务不变量
   - 哪些规则任何时候都成立
   ↓
5. 列状态机(状态 + 转换)
   ↓
6. 用充血模型实现
   - 私有构造 + 工厂方法
   - 业务方法替代 setter
   - 不变量在构造时校验
   ↓
7. 写单元测试(核心)
   - 业务规则测试
   - 状态转换测试
   - 不变量违反测试
   ↓
8. Service 层只做编排

配套模板

  • templates/domain-model-template.md — 实体 + 值对象 + 聚合 + 状态机 + 不变量 + 业务方法 + 测试

质量自检

text
□ 实体 vs 值对象 区分清楚
□ 聚合根明确
□ 业务规则在 Domain,不在 Service
□ 用业务方法名(cancel/pay)替代 setter
□ 私有构造 + 工厂方法
□ 不变量在构造时校验
□ 状态机定义所有合法转换
□ 非法状态转换抛异常
□ 值对象不可变
□ 聚合事务边界 = 一个聚合
□ Domain 层不依赖框架
□ 单元测试覆盖业务规则

常见坑

  1. 贫血模型——业务逻辑全在 Service,Domain 退化为数据容器
  2. 暴露 setter——绕过业务规则
  3. 金额用 float——精度问题
  4. 状态用 String——编译期不安全
  5. 状态转换分散——同样规则多处实现
  6. 聚合事务跨多个——一致性难保证
  7. 值对象可变——共享时被改坏
  8. 业务规则在 Controller——重复 / 难测
  9. 构造函数不校验不变量——可创建非法对象
  10. Domain 依赖 Spring/NestJS——失去独立性

与其他 skill 的协作

text
上游:
  product-manager 工作流 → 业务实体 / 状态 / 规则
  database 工作流 → 数据模型映射

下游:
  api-implementation → Service 编排 Domain
  data-access → Domain ↔ ORM 映射
  testing-implementation → 单元测试 Domain

相关参考

  • 项目根 references/backend-frameworks-2026.md — 框架与建模风格关系

File inclusi

Il modello legge questi file quando servono, mentre la skill è caricata. Sono accessibili in sola lettura e non vengono mai eseguiti.

Domande frequenti

Cosa fa la skill IA Domain Modeling?

设计业务领域模型 / 实体 / 值对象 / 聚合 / 业务规则时使用。覆盖所有主流后端语言(Java / TS / Python / Go / Ruby / C#)。融合 DDD(Eric Evans)+ 充血模型 + 状态机模式。

Perché usare Domain Modeling su TypingMind?

Perché la installi una volta e la usi con qualsiasi modello. Domain Modeling è scritta in semplice Markdown anziché in codice specifico di un fornitore, quindi la stessa skill funziona con GPT-5, Claude, Gemini, Grok o un modello locale, e puoi cambiare modello durante la chat senza che la skill smetta di funzionare. TypingMind usa le tue chiavi API, quindi paghi direttamente i fornitori anziché un abbonamento per posto utente, e le tue skill e chat restano nel tuo spazio di archiviazione.

Come installo Domain Modeling in TypingMind?

Apri Plugin → Skill → Installa da GitHub in TypingMind e incolla https://github.com/zhaoxuya520/AI-Fullstack-Delivery-Workflow/tree/master/workflows/backend-engineer/skills/domain-modeling. TypingMind legge il file SKILL.md, include i file associati e installa la skill, che puoi abilitare per ogni singola chat.

Quali modelli IA possono usare Domain Modeling?

Qualsiasi modello che colleghi a TypingMind. Le skill IA sono semplici istruzioni in Markdown anziché codice specifico di un fornitore, quindi GPT, Claude, Gemini, Grok e i modelli locali possono tutti caricare questa skill quando una richiesta corrisponde al suo scopo.

Quanti modelli IA posso usare con Domain Modeling?

Quanti ne vuoi. Se un modello supporta le skill, puoi usarlo con Domain Modeling: GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama e molti altri, tutti su TypingMind con le tue chiavi API.

La skill IA Domain Modeling è gratuita?

È pubblicata su GitHub da zhaoxuya520. Consulta il repository per i termini di licenza. Paghi solo il tuo fornitore IA per i token che usi.

Cosa sono le skill IA?

Una skill IA è un pacchetto di istruzioni riutilizzabili che insegna a un modello IA come svolgere un'attività specifica. Segue il formato aperto Agent Skills: un file SKILL.md con un nome e una descrizione, più eventuali script, template o file di riferimento di cui il modello potrebbe aver bisogno. Il modello legge le istruzioni solo quando la tua richiesta corrisponde alla skill, quindi una skill installata non comporta costi finché non viene usata.

In cosa si differenziano le skill IA dai plugin o dai server MCP?

Un plugin o un server MCP fornisce a un modello nuovi strumenti da richiamare: codice che viene eseguito da qualche parte e restituisce un risultato. Una skill IA, invece, fornisce al modello conoscenze e procedure: come affrontare un'attività, quali passaggi seguire e quali caratteristiche deve avere un buon output. Le skill sono semplici istruzioni in Markdown, quindi non richiedono server, chiavi API né ambienti di esecuzione e funzionano con qualsiasi modello.

Elenco completo

Crea subito il tuo spazio di lavoro AI

Iscriviti alla nostra newsletter per ricevere aggiornamenti sulle nuove funzionalità e sulle prossime iniziative a premi 👇