{"id":7042,"date":"2026-03-26T18:53:01","date_gmt":"2026-03-26T10:53:01","guid":{"rendered":"https:\/\/t.n-years.com\/?p=7042"},"modified":"2026-03-26T18:53:01","modified_gmt":"2026-03-26T10:53:01","slug":"ddd-%e6%a1%88%e4%be%8b%ef%bc%9a%e8%ae%a2%e5%8d%95%e5%92%8c%e4%bc%98%e6%83%a0%e5%88%b8","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=7042","title":{"rendered":"DDD \u6848\u4f8b\uff1a\u8ba2\u5355\u548c\u4f18\u60e0\u5238"},"content":{"rendered":"<blockquote>\n<p>Gemini \u751f\u6210<\/p>\n<\/blockquote>\n<p>\u65e2\u7136\u4f60\u51c6\u5907\u597d\u6df1\u5165\u4e86\uff0c\u90a3\u6211\u4eec\u5c31\u62ff<strong>\u201c\u4f18\u60e0\u5238\u6838\u9500\uff08Coupon Redemption\uff09\u201d<\/strong>\u8fd9\u4e2a\u6781\u5177\u4ee3\u8868\u6027\u7684\u573a\u666f\uff0c\u6765\u4e00\u573a\u67b6\u6784\u5e08\u7ea7\u522b\u7684\u201c\u624b\u672f\u5f0f\u201d\u62c6\u89e3\u3002<\/p>\n<p>\u5728\u4f20\u7edf\u5f00\u53d1\u4e2d\uff0c\u4f60\u53ef\u80fd\u76f4\u63a5\u5728 <code>OrderService<\/code> \u91cc\u5199\u4e00\u6bb5\u903b\u8f91\uff1a\u6821\u9a8c\u4f18\u60e0\u5238\u3001\u6263\u51cf\u4f59\u989d\u3001\u6539\u8ba2\u5355\u72b6\u6001\u3002\u4f46\u5728 DDD \u89c6\u89d2\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u56de\u7b54\u4e00\u4e2a\u6838\u5fc3\u95ee\u9898\uff1a<strong>\u4f18\u60e0\u5238\u6838\u9500\uff0c\u5230\u5e95\u662f\u5c5e\u4e8e\u201c\u8ba2\u5355\u201d\u805a\u5408\uff0c\u8fd8\u662f\u201c\u4f18\u60e0\u5238\u201d\u805a\u5408\uff1f<\/strong><\/p>\n<hr \/>\n<h3>1. \u65b9\u6848\u6743\u8861\uff1a\u8c01\u624d\u662f\u201c\u805a\u5408\u6839\u201d\uff1f<\/h3>\n<p>\u8fd9\u662f DDD \u843d\u5730\u65f6\u6700\u7ecf\u5178\u7684\u4e89\u8bba\u3002\u6211\u4eec\u6765\u770b\u4e24\u79cd\u5efa\u6a21\u8def\u5f84\u7684 <strong>Pros &amp; Cons<\/strong>\uff1a<\/p>\n<h4>\u65b9\u6848 A\uff1a\u4f18\u60e0\u5238\u4f5c\u4e3a\u8ba2\u5355\uff08Order\uff09\u7684\u4e00\u90e8\u5206<\/h4>\n<ul>\n<li><strong>\u601d\u8def\uff1a<\/strong> \u5c06\u4f18\u60e0\u5238\u4fe1\u606f\u5b58\u653e\u5728 <code>Order<\/code> \u805a\u5408\u5185\u90e8\uff0c\u4f5c\u4e3a\u5176\u5b9e\u4f53\u3002<\/li>\n<li><strong>Pros\uff1a<\/strong> \u5f3a\u4e00\u81f4\u6027\u3002\u5728\u4e00\u4e2a\u6570\u636e\u5e93\u4e8b\u52a1\u91cc\u5b8c\u6210\u8ba2\u5355\u521b\u5efa\u548c\u4f18\u60e0\u5238\u4f7f\u7528\uff0c\u7edd\u5bf9\u4e0d\u4f1a\u51fa\u73b0\u201c\u8ba2\u5355\u4ed8\u4e86\u94b1\u4f46\u4f18\u60e0\u5238\u6ca1\u6263\u201d\u7684\u60c5\u51b5\u3002<\/li>\n<li><strong>Cons\uff1a<\/strong> \u805a\u5408\u8fc7\u4e8e\u81c3\u80bf\u3002\u5982\u679c\u4f18\u60e0\u5238\u8fd8\u6709\u81ea\u5df1\u7684\u590d\u6742\u903b\u8f91\uff08\u5982\uff1a\u8fc7\u671f\u81ea\u52a8\u9000\u56de\u3001\u52a8\u6001\u8c03\u6574\u6298\u6263\uff09\uff0c\u8ba2\u5355\u6a21\u578b\u4f1a\u53d8\u5f97\u6781\u5176\u590d\u6742\u3002<\/li>\n<\/ul>\n<h4>\u65b9\u6848 B\uff1a\u4f18\u60e0\u5238\uff08Coupon\uff09\u662f\u72ec\u7acb\u805a\u5408\uff08\u63a8\u8350\uff09<\/h4>\n<ul>\n<li><strong>\u601d\u8def\uff1a<\/strong> \u8ba2\u5355\u548c\u4f18\u60e0\u5238\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u805a\u5408\u6839\u3002<\/li>\n<li><strong>Pros\uff1a<\/strong> \u804c\u8d23\u6e05\u6670\uff0c\u7b26\u5408<strong>\u9ad8\u5185\u805a\u4f4e\u8026\u5408<\/strong>\u3002\u4f18\u60e0\u5238\u53ef\u4ee5\u72ec\u7acb\u6f14\u8fdb\uff08\u6bd4\u5982\u589e\u52a0\u201c\u62c9\u65b0\u4e13\u9879\u5238\u201d\u903b\u8f91\uff09\uff0c\u4e0d\u5f71\u54cd\u8ba2\u5355\u3002<\/li>\n<li><strong>Cons\uff1a<\/strong> \u5b58\u5728\u5206\u5e03\u5f0f\u4e00\u81f4\u6027\u6311\u6218\u3002\u9700\u8981\u5904\u7406\u201c\u8ba2\u5355\u521b\u5efa\u6210\u529f\u4f46\u4f18\u60e0\u5238\u9501\u5b9a\u5931\u8d25\u201d\u7684\u56de\u6eda\u903b\u8f91\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>2. \u6218\u672f\u5b9e\u73b0\uff1a\u5982\u4f55\u4f18\u96c5\u5730\u201c\u6838\u9500\u201d\uff1f<\/h3>\n<p>\u5982\u679c\u91c7\u7528\u65b9\u6848 B\uff0c\u6211\u4eec\u9700\u8981\u5f15\u5165<strong>\u9886\u57df\u4e8b\u4ef6\uff08Domain Event\uff09<\/strong>\u3002<\/p>\n<h4>\u6b65\u9aa4\u4e00\uff1a\u5b9a\u4e49\u805a\u5408\u7684\u884c\u4e3a<\/h4>\n<ul>\n<li><strong>Coupon Aggregate:<\/strong> \u66b4\u9732 <code>lock()<\/code> \u548c <code>redeem()<\/code> \u65b9\u6cd5\u3002\u5185\u90e8\u7ef4\u62a4\u72b6\u6001\u673a\uff08Available -&gt; Locked -&gt; Used\uff09\u3002<\/li>\n<li><strong>Order Aggregate:<\/strong> \u66b4\u9732 <code>create()<\/code> \u65b9\u6cd5\u3002\u5185\u90e8\u5305\u542b <code>AppliedCouponId<\/code>\u3002<\/li>\n<\/ul>\n<h4>\u6b65\u9aa4\u4e8c\uff1a\u6d41\u7a0b\u7f16\u6392\uff08Application Service\uff09<\/h4>\n<p>\u5e94\u7528\u5c42\u4e0d\u8d1f\u8d23\u4e1a\u52a1\u903b\u8f91\uff0c\u53ea\u8d1f\u8d23<strong>\u201c\u53d1\u53f7\u65bd\u4ee4\u201d<\/strong>\uff1a<\/p>\n<ol>\n<li>\u8c03\u7528 <code>CouponRepository<\/code> \u52a0\u8f7d\u4f18\u60e0\u5238\u3002<\/li>\n<li>\u6267\u884c <code>coupon.lock()<\/code>\uff08\u5185\u5b58\u4e2d\u72b6\u6001\u53d8\u66f4\uff09\u3002<\/li>\n<li>\u8c03\u7528 <code>OrderRepository<\/code> \u4fdd\u5b58\u8ba2\u5355\u3002<\/li>\n<li>\u6267\u884c <code>couponRepository.save(coupon)<\/code> \u6301\u4e45\u5316\u3002<\/li>\n<\/ol>\n<hr \/>\n<h3>3. \u6838\u5fc3\u6311\u6218\uff1a\u5982\u4f55\u5e94\u5bf9\u201c\u5e76\u53d1\u6838\u9500\u201d\uff1f<\/h3>\n<p>\u5728 10 \u500d\u6d41\u91cf\u4e0b\uff0c\u5982\u679c\u4e24\u4e2a\u8bf7\u6c42\u540c\u65f6\u7528\u4e00\u5f20\u5238\u600e\u4e48\u529e\uff1f<\/p>\n<ul>\n<li><strong>DDIA \u601d\u60f3\u5e94\u7528\uff1a<\/strong> \u6211\u4eec\u4e0d\u4f9d\u8d56\u6570\u636e\u5e93\u7684\u5927\u9501\uff0c\u800c\u662f\u5229\u7528<strong>\u4e50\u89c2\u9501\uff08Versioning\uff09<\/strong>\u3002<\/li>\n<li><strong>DDD \u5904\u7406\uff1a<\/strong> \u5728\u805a\u5408\u6839\u4fdd\u5b58\u65f6\u6821\u9a8c\u7248\u672c\u53f7\u3002\u5982\u679c\u7248\u672c\u51b2\u7a81\uff0c\u8bf4\u660e\u5238\u5df2\u88ab\u4ed6\u4eba\u62a2\u5148\u9501\u5b9a\uff0c\u76f4\u63a5\u629b\u51fa\u9886\u57df\u5f02\u5e38 <code>CouponAlreadyUsedException<\/code>\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>4. \u5173\u952e\u67b6\u6784\u56fe\uff08\u63cf\u8ff0\uff09<\/h3>\n<ol>\n<li><strong>\u7528\u6237\u53d1\u8d77\u8bf7\u6c42\uff1a<\/strong> \u643a\u5e26 <code>couponId<\/code>\u3002<\/li>\n<li><strong>\u5e94\u7528\u670d\u52a1\uff08App Service\uff09\uff1a<\/strong> \u5f00\u542f\u4e8b\u52a1\u3002<\/li>\n<li><strong>\u9886\u57df\u5c42\uff08Domain\uff09\uff1a<\/strong> * <code>Coupon<\/code> \u805a\u5408\u6821\u9a8c\u6709\u6548\u671f\u548c\u9002\u7528\u8303\u56f4\u3002\n<ul>\n<li><code>Order<\/code> \u805a\u5408\u8ba1\u7b97\u6700\u7ec8\u4ef7\u683c\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u57fa\u7840\u8bbe\u65bd\u5c42\uff08Infrastructure\uff09\uff1a<\/strong> * Repository \u6267\u884c SQL\uff1a<code>UPDATE coupon SET status = &#039;LOCKED&#039; WHERE id = ? AND version = ?<\/code>\u3002<\/li>\n<li><strong>\u5f02\u6b65\u8865\u507f\uff1a<\/strong> \u5982\u679c\u8ba2\u5355\u652f\u4ed8\u8d85\u65f6\uff0c\u53d1\u5e03 <code>OrderCancelledEvent<\/code>\uff0c\u4f18\u60e0\u5238\u805a\u5408\u76d1\u542c\u540e\u81ea\u52a8\u6267\u884c <code>unlock()<\/code>\u3002<\/li>\n<\/ol>\n<hr \/>\n<h3>5. \u6f14\u8fdb\u5efa\u8bae\uff1a\u4ece\u201c\u540c\u6b65\u201d\u8f6c\u5411\u201c\u4e8b\u4ef6\u9a71\u52a8\u201d<\/h3>\n<p>\u968f\u7740\u4e1a\u52a1\u91cf\u589e\u957f\uff0c\u540c\u6b65\u9501\u5b9a\u4f18\u60e0\u5238\u4f1a\u62d6\u6162\u8ba2\u5355\u521b\u5efa\u3002<\/p>\n<ul>\n<li><strong>10 \u500d\u6d41\u91cf\u7b56\u7565\uff1a<\/strong> \u91c7\u7528 <strong>\u9884\u6263\u6a21\u5f0f<\/strong>\u3002\n<ul>\n<li>\u7528\u6237\u4e0b\u5355\u65f6\uff0c\u5148\u53d1\u4e00\u4e2a\u5f02\u6b65\u6d88\u606f\u53bb\u9501\u5b9a\u4f18\u60e0\u5238\u3002<\/li>\n<li>\u8ba2\u5355\u7cfb\u7edf\u8fdb\u5165\u201c\u5f85\u786e\u8ba4\u201d\u72b6\u6001\u3002<\/li>\n<li>\u5229\u7528 <strong>Saga \u6a21\u5f0f<\/strong> \u6216 <strong>\u672c\u5730\u6d88\u606f\u8868<\/strong> \u786e\u4fdd\u4e24\u4e2a\u805a\u5408\u4e4b\u95f4\u7684\u6700\u7ec8\u4e00\u81f4\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<p><strong>\u6700\u540e\u7684\u4e00\u70b9\u8001\u5175\u5fc3\u5f97\uff1a<\/strong><br \/>\nDDD \u4e0d\u662f\u4e3a\u4e86\u8ba9\u4ee3\u7801\u53d8\u591a\uff0c\u800c\u662f\u4e3a\u4e86\u8ba9<strong>\u4e1a\u52a1\u89c4\u5219\u6709\u5730\u65b9\u843d\u811a<\/strong>\u3002\u5982\u679c\u4f60\u53d1\u73b0\u4f60\u7684 <code>Service<\/code> \u4f9d\u7136\u6709\u51e0\u5343\u884c\uff0c\u800c <code>Entity<\/code> \u53ea\u6709 Getter\/Setter\uff0c\u90a3\u4e48\u4f60\u53ea\u662f\u62ab\u7740 DDD \u5916\u58f3\u7684\u4f20\u7edf\u5f00\u53d1\u3002<\/p>\n<p><strong>\u8fd9\u4e2a\u4f18\u60e0\u5238\u6838\u9500\u7684\u903b\u8f91\uff0c\u4f60\u662f\u5e0c\u671b\u6211\u7528\u4f2a\u4ee3\u7801\u5c55\u793a\u4e00\u4e0b\u201c\u5145\u8840\u6a21\u578b\u201d\u7684\u5177\u4f53\u5199\u6cd5\uff0c\u8fd8\u662f\u60f3\u804a\u804a\u8fd9\u79cd\u67b6\u6784\u4e0b\u5982\u4f55\u505a\u5355\u5143\u6d4b\u8bd5\uff1f<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Gemini \u751f\u6210 \u65e2\u7136\u4f60\u51c6\u5907\u597d\u6df1\u5165\u4e86\uff0c\u90a3\u6211\u4eec\u5c31\u62ff\u201c\u4f18\u60e0\u5238\u6838\u9500\uff08Coupon Redemption\uff09\u201d\u8fd9\u4e2a\u6781\u5177&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=7042\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[],"class_list":["post-7042","post","type-post","status-publish","format-standard","hentry","category-123"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/7042","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7042"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/7042\/revisions"}],"predecessor-version":[{"id":7043,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/7042\/revisions\/7043"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}