领域服务 logo

领域服务

Organization
microwind
领域服务

封装不属于任何单个实体或值对象的领域逻辑,是无状态的纯业务操作。

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-service
  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-service .claude/skills/microwind-65
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 Service)

概述

领域服务封装不自然属于任何实体或值对象的领域逻辑。Eric Evans 给出三条判别原则——满足这三条,才应将一段逻辑建模为领域服务:

  1. 操作表达的是一个领域概念,但该概念不自然归属于任何 Entity 或 Value Object;
  2. 接口用领域语言定义,参数与返回值都是领域类型;
  3. 操作是无状态的——同一服务实例可以被多个上下文复用。

何时使用领域服务:

  • 操作涉及多个聚合(如转账、价格计算)
  • 业务逻辑不属于任何一个实体(强行放入会破坏单一职责)
  • 需要使用外部服务但仍属领域语义(通过领域接口 + 防腐层)

⚠️ 优先把行为放在实体或值对象中;只有当上述三条都成立时,才使用领域服务作为补充。

代码示例

java
// ✅ 领域服务:转账涉及两个账户聚合
public class TransferService {  // 领域服务,无状态
    public void transfer(Account from, Account to, Money amount) {
        if (!from.hasSufficientFunds(amount)) {
            throw new InsufficientFundsException();
        }
        from.debit(amount);
        to.credit(amount);
    }
}

// ❌ 错误:把转账逻辑放在 Account 实体中
public class Account {
    public void transferTo(Account target, Money amount) {
        // Account 不应该知道另一个 Account 的存在
        this.debit(amount);
        target.credit(amount);  // 直接操作另一个聚合
    }
}
python
# ✅ 领域服务:价格计算涉及多个因素
class PricingService:
    """领域服务:计算最终价格"""

    def calculate_price(self, product, customer, coupon=None) -> Money:
        base_price = product.price

        # 会员折扣
        if customer.is_vip():
            base_price = base_price.multiply(0.9)

        # 优惠券
        if coupon and coupon.is_valid():
            base_price = coupon.apply(base_price)

        return base_price

领域服务 vs 应用服务

维度领域服务应用服务
职责纯业务逻辑用例编排
状态无状态无状态
依赖只依赖领域对象依赖领域服务、仓储等
事务不管理事务管理事务
层次领域层应用层
java
// 领域服务:纯业务逻辑
public class DiscountService {
    public Money calculateDiscount(Order order, Customer customer) {
        // 纯领域逻辑,无基础设施依赖
    }
}

// 应用服务:编排流程
@Service
@Transactional
public class OrderApplicationService {
    private final OrderRepository orderRepo;
    private final DiscountService discountService;

    public OrderDTO placeOrder(PlaceOrderCommand cmd) {
        Order order = orderRepo.findById(cmd.getOrderId());
        Customer customer = customerRepo.findById(cmd.getCustomerId());
        Money discount = discountService.calculateDiscount(order, customer);
        order.applyDiscount(discount);
        order.place();
        orderRepo.save(order);
        return OrderDTO.from(order);
    }
}

常见误区

❌ 用领域服务替代实体行为——OrderService.cancel(order) 替代 order.cancel() → 行为优先回归实体;只有跨聚合或不属于任何实体的逻辑才下沉到领域服务

❌ 领域服务直接调用基础设施——里面注入 HTTP Client、JdbcTemplate → 领域服务只依赖领域接口;外部技术细节由基础设施层适配器实现

❌ 领域服务变成"杂物间 Service"——业务逻辑、参数转换、事务、权限全往里塞 → 领域服务只做"纯领域计算";编排、事务、权限属于应用层

与其他DDD概念的关系

概念关系
实体领域服务操作实体但不替代实体的方法
应用服务应用服务调用领域服务
领域模型领域服务是领域模型的一部分

总结

核心:领域服务封装跨实体的纯业务逻辑,无状态。

判断标准:如果逻辑不自然属于任何一个实体,就用领域服务。优先把逻辑放在实体中,领域服务是补充。

Frequently asked questions

What does the 领域服务 AI skill do?

封装不属于任何单个实体或值对象的领域逻辑,是无状态的纯业务操作。

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-service. 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 👇