{"id":844,"date":"2024-02-25T10:36:31","date_gmt":"2024-02-25T09:36:31","guid":{"rendered":"http:\/\/blog.chen-hongyi.com\/?p=844"},"modified":"2024-02-25T10:36:31","modified_gmt":"2024-02-25T09:36:31","slug":"%e5%88%9d%e8%af%86ddd-%e4%b9%8bcqrs","status":"publish","type":"post","link":"https:\/\/chen-hongyi.com\/?p=844","title":{"rendered":"\u521d\u8bc6DDD \u4e4bCQRS"},"content":{"rendered":"\n<p>\u6700\u8fd1\u5728\u5b66\u4e60\u5b9e\u8df5DDD\uff0c\u63a5\u89e6\u5230\u4e00\u4e2a\u65b0\u7684\u6982\u5ff5CQRS\u3002\u4ee5\u524d\u4ee5\u4e3a\u5c31\u662f\u8bfb\u5199\u5206\u79bb\uff0c\u662f\u57fa\u7840\u8bbe\u65bd\u5c42\u6570\u636e\u5e93\u5c42\u9762\u7684\u4e8b\u60c5\u3002\u5b66\u4e60\u4ee5\u540e\u624d\u53d1\u73b0\uff0c\u5e76\u4e0d\u53ea\u662f\u8fd9\u6837\uff0c\u66f4\u5e94\u8be5\u662fApp\u5e94\u7528\u5c42\u9762\u7684\u4e8b\u3002<\/p>\n\n\n\n<p>\u7f18\u8d77\u4ecegithub\u4e0a\u627e\u4e86\u4e00\u4e2aDDD\u7684symfony\u7684<a href=\"https:\/\/github.com\/CodelyTV\/php-ddd-example\">\u4f8b\u5b50<\/a>\uff0c\u60f3\u53c2\u8003\u4e00\u4e0b\uff0c\u7528\u4e8e\u91cd\u6784\u76ee\u524d\u7684\u9879\u76ee\u3002\u770b\u4e86\u4ecb\u7ecd\u548c\u4ee3\u7801\u4ee5\u540e\uff0c\u53d1\u73b0\u4e86\u4ed6\u7684\u4f5c\u8005\u7684\u897f\u73ed\u7259\u8bed\u7684\u6cb9\u7ba1\u89c6\u9891\u3002\u7ed3\u5408\u81ea\u52a8\u5b57\u5e55\u7ffb\u8bd1\uff0c\u89c9\u5f97\u8fd8\u4e0d\u9519\uff0c\u7136\u540e\u770b\u4e86\u4e00\u4e0b\u4ed6\u4eec\u5199\u7684\u4e66\u3002\u539f\u7248\u898160\u6b27\uff0c\u4e8e\u662f\u5728github\u4e0a\u4e0b\u8f7d\u4e86\u4e00\u672c\u7535\u5b50\u7248 <a href=\"https:\/\/github.com\/shubham-shinde\/books\/blob\/master\/PHP\/Domain-Driven%20Design%20in%20PHP%20-%20Buenosvinos%2C%20Carlos%3B%20Soronellas%2C%20Christian.pdf\">Domian Driven Desgin  in PHP<\/a>\u3002\u4ee5\u4e0b\u662f\u9605\u8bfb\u7684\u7b14\u8bb0\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>MVC\u6a21\u5f0f\u7684\u7f3a\u70b9<\/strong><\/p>\n\n\n\n<p>MVC\u7528\u4e86\u90a3\u4e48\u4e45\u4e86\uff0c\u6700\u5927\u95ee\u9898\u662fcontroller\u8fc7\u4e8e\u5e9e\u5927\u3002\u8fdd\u53cd\u4e86\u5206\u79bb\u539f\u5219(Separation of Concerns)\u3002\u8865\u6551\u7684\u529e\u6cd5\u662f\u4f7f\u7528\u4f9d\u8d56\u5012\u7f6e\u3002\u7531\u4e8econtroller\u5305\u542b\u4e86\u8fc7\u591a\u7684\u9886\u57df\u5185\u5bb9\u3002\u6240\u4ee5\u9996\u5148\u62ff\u9886\u57df\u6a21\u578b\u4e0b\u624b\u3002\u5f88\u591a\u9886\u57df\u6a21\u578b\u4f9d\u8d56\u5e95\u5c42\u7684\u57fa\u7840\u67b6\u6784\uff0c\u6bd4\u5982\u6570\u636e\u5e93\u3002\u6240\u4ee5\u6309\u7167Bob\u5927\u53d4(Robert C. Martin\uff09\u7684\u4f9d\u8d56\u5012\u7f6e\u539f\u5219\uff0c\u9ad8\u7ea7\u6a21\u5757\uff08\u9886\u57df\uff09\u4e0d\u8be5\u4f9d\u8d56\u4f4e\u7ea7\u6a21\u5757\uff08\u57fa\u7840\u8bbe\u65bd\uff09\uff0c\u800c\u4e14\u5e94\u8be5\u8ba9\u5b83\u4f9d\u8d56\u62bd\u8c61\uff0c\u5373\u63d0\u51faRepository\u3002\u7136\u540e<strong>\u516d\u89d2\u5f62\u67b6\u6784<\/strong>\u88ab\u63d0\u51fa\u4e86\u3002\u6240\u6709\u624d\u6709\u540e\u6765\u7684<strong> CQRS <\/strong>\u548c <strong>Event Sourcing<\/strong> \u3002<\/p>\n\n\n\n<p><strong>\u521d\u8bc6DDD \u4e4bCQRS<\/strong><\/p>\n\n\n\n<p><strong>\u5199\u6a21 \u578b Write model<\/strong>.<\/p>\n\n\n\n<p>\u4e5f\u53ebCommand model\uff0c 1<\/p>\n\n\n\n<p> \u88ab\u6539\u9020\u4e3a\u57fa\u4e8e\u4e8b\u4ef6\u7684\u5199\u6a21\u578b\u3002\u672c\u6765\u76f4\u63a5\u5199\u5165\u6570\u636e\u5e93\u52a8\u4f5c\uff0c\u6539\u4e3a\u53d1\u5e03\u4e00\u4e2a\u5199\u5165\u4e8b\u4ef6\u3002<\/p>\n\n\n\n<p>record -&gt; apply -&gt; publish<\/p>\n\n\n\n<p>\u800c\u4e14\u5199\u5165\u4e8b\u4ef6\u76f4\u63a5\u627e\u9886\u57df\u6a21\u578b\u91cc\u5b9a\u4e49\u3002<\/p>\n\n\n\n<p><strong>\u8bfb\u6a21\u578b Read model<\/strong><\/p>\n\n\n\n<p>\u88ab\u7b80\u5316\u4e3a\u4eceElasticSerach\u8fd9\u6837\u7684\u7f13\u5b58\u8bfb\u53d6\u3002<\/p>\n\n\n\n<p><strong>\u540c\u6b65<\/strong><\/p>\n\n\n\n<p>\u5373\u7136\u8bfb\u5199\u5206\u5f00\u4e86\uff0c\u90a3\u4e48\u4e24\u4e2a\u6a21\u578b\u5982\u4f55\u5b9e\u73b0\u540c\u6b65\u5462\u3002\u5f15\u5165\u4e86\u65b0\u6982\u5ff5Projection \uff08\u6295\u5c04\u5668\uff1f\uff09\u3002\u57fa\u672c\u4e0a\u5c31\u662f\u76d1\u542c\u4e8b\u4ef6\u3002\u5f53\u7b26\u5408\u7684\u4e8b\u4ef6\u88ab\u76d1\u542c\u5230\u4e86\uff0c\u5c31\u4f1a\u8c03\u7528\u6295\u5c04\u5668\u7684\u6295\u5c04\u52a8\u4f5c\u3002<\/p>\n\n\n\n<p>\u6295\u5c04\u7684\u8fc7\u7a0b\u5f88\u53ef\u80fd\u662f\u5f02\u6b65\u7684\uff0c\u5c31\u662f\u8bf4\u4f1a\u6709\u5ef6\u8fdf\u3002\u4e3a\u4e86\u89e3\u51b3\u5ef6\u8fdf\uff0c\u5728Repo\u91cc\u7684save\u65b9\u6cd5\u5185\uff0c\u521b\u5efa\u4e00\u4e2atransaction\uff08\u8fc7\u7a0b\uff09\uff0c\u628a\u5b58\u50a8\uff0c\u4e8b\u4ef6\u653e\u5728\u4e00\u8d77\u3002\u7136\u540e\u7528\u6295\u5c04\u5668\u6295\u5c04\u4fdd\u5b58\u7684\u4e8b\u4ef6\u3002<\/p>\n\n\n\n<p>\u6700\u7ec8\u89e3\u51b3\u4e86\u8bfb\u6a21\u578b\u548c\u5199\u6a21\u578b\u4e4b\u95f4\u7684\u4e00\u81f4\u6027(consistencies)\u95ee\u9898\u3002<\/p>\n\n\n\n<p><strong>Evnet Sourcing \u4e8b\u4ef6\u6eaf\u6e90<\/strong><\/p>\n\n\n\n<p>\u5c31\u662f\u53ef\u4ee5\u901a\u8fc7\u8bb0\u5f55\u5b8c\u6574\u7684event\u53ef\u4ee5\u8fd8\u539f\u4e00\u4e2a\u72b6\u6001\u3002\u6709\u70b9\u50cf\u6570\u636e\u5e93\u7684bin log\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u5b66\u4e60\u5b9e\u8df5DDD\uff0c\u63a5\u89e6\u5230\u4e00\u4e2a\u65b0\u7684\u6982\u5ff5CQRS\u3002\u4ee5\u524d\u4ee5\u4e3a\u5c31\u662f\u8bfb\u5199\u5206\u79bb\uff0c\u662f\u57fa\u7840\u8bbe &hellip; <a href=\"https:\/\/chen-hongyi.com\/?p=844\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[118,120,119],"class_list":["post-844","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-cqrs","tag-ddd","tag-119"],"_links":{"self":[{"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/posts\/844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=844"}],"version-history":[{"count":6,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/posts\/844\/revisions"}],"predecessor-version":[{"id":960,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/posts\/844\/revisions\/960"}],"wp:attachment":[{"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=844"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=844"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}