惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Latest
Security Latest
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
GbyAI
GbyAI
云风的 BLOG
云风的 BLOG
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
宝玉的分享
宝玉的分享
博客园 - 【当耐特】
C
Cyber Attacks, Cyber Crime and Cyber Security
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MyScale Blog
MyScale Blog
T
Tor Project blog
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
P
Privacy & Cybersecurity Law Blog
MongoDB | Blog
MongoDB | Blog
Simon Willison's Weblog
Simon Willison's Weblog
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
小众软件
小众软件
G
GRAHAM CLULEY
P
Privacy International News Feed
AWS News Blog
AWS News Blog
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
人人都是产品经理
人人都是产品经理
S
Schneier on Security
Scott Helme
Scott Helme
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
B
Blog RSS Feed
T
The Exploit Database - CXSecurity.com
Recent Announcements
Recent Announcements
E
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
U
Unit 42
The Register - Security
The Register - Security
S
Securelist
Martin Fowler
Martin Fowler
Project Zero
Project Zero
大猫的无限游戏
大猫的无限游戏
Cisco Talos Blog
Cisco Talos Blog

博客园 - mabiao008

CC skill Mac下载免费软件遇到问题解决 错误码code是int类型好还是String类型好 idea2025版本破解 pgsql切换当前会话的模式 java程序中增加mongodb的联合索引 文档启动脚本报错:-bash: ./app.sh: /bin/bash^M: bad interpreter: No such file or directory Milvus索引 pandoc使用 idea把unicode转为中文 idea 插件分享 java项目处理OFD文件 java项目无法读取resources目录下的文件 三种数据对象的区别 Linux环境aspose插件word转pdf中文乱码解决方案 java List报错Method threw ‘java.lang.UnsupportedOperationException‘ exception. 解决 java字段值为null,转json后不存在该字段对应的key 格式化json大文件 springboot项目启动报错Command line is too long. Shorten the command line via JAR manifest or via a classpath file and rerun. com.alibaba.excel.exception.ExcelGenerateException: Can not close IO linux的TCP端口问题 关于linux端口号 linux下生成pdf文件名遇到问题 把页面查询到的数据导出PDF文件(html中的在线表单下载为pdf文件)
CQRS
mabiao008 · 2026-05-15 · via 博客园 - mabiao008

CQRS(Command Query Responsibility Segregation)是命令与查询职责分离的模式。

核心思想:把数据的写操作(Command)和读操作(Query)分开处理。

传统方式 vs CQRS
传统:同一个 Service/Repository 既读又写
┌────────────────┐
│ BudgetService │
│ ├─ getById() │ ← 读
│ ├─ save() │ ← 写
│ ├─ update() │ ← 写
│ └─ delete() │ ← 写
│ └─ list() │ ← 读
└────────────────────┘

CQRS:读写分离
┌────────────────┐ ┌────────┐
│ Query 侧 │ Command 侧 │
│ ├─ getById() │ │ ├─ save() │
│ ├─ list() │ │ ├─ update() │
│ ├─ search() │ │ ├─ delete() │
│ └─ 绑定查询模型 │ │ └─ 绑定写模型 │
│ (BudgetControlModel)│ │ (BudgetPackageControl)│
└────────────────────┘ └────────────────┘

我们项目中的应用
刚才的重构其实就是 CQRS 思路:

| 层 | 职责 | 模型 | |----|------|

| Repository(查询侧) | 只做数据读取,返回 Entity | BudgetPackageControl |

| Service(命令侧) | 业务编排:校验、ID生成、事务控制 | BudgetControlModel / BudgetControlSaveDTO |

好处:

读侧可以优化查询性能(比如用不同的 SQL、缓存、甚至不同的数据库)
写侧专注业务逻辑和事务一致性
模型解耦:查询返回的 Model 和写入用的 DTO/Entity 可以不一样,各自演化不互相影响

什么时候该用 CQRS?
| 场景 | 建议 | |------|

| 简单 CRUD | 不需要,传统方式够用 |

| 读多写少,读逻辑复杂 | 适合,查询侧可以独立优化 | | 写操作有复杂业务编排 | 适合,命令侧集中管理事务 | | 读写模型差异大 | 适合,避免一个模型既满足读又满足写 |

我们项目当前属于"写操作有复杂业务编排"的场景(copy、batchSave 涉及多表事务),所以用 CQRS 分开是合理的。