{"id":686,"date":"2021-04-19T16:09:12","date_gmt":"2021-04-19T15:09:12","guid":{"rendered":"http:\/\/blog.chen-hongyi.com\/?p=686"},"modified":"2021-04-21T08:54:40","modified_gmt":"2021-04-21T07:54:40","slug":"entity-repository-and-doctirne","status":"publish","type":"post","link":"https:\/\/chen-hongyi.com\/?p=686","title":{"rendered":"Entity,  Repository and Doctirne"},"content":{"rendered":"\n<p>\u4e09\u7bc7\u6587\u7ae0\u5e2e\u4f60\u7406\u89e3Repository\u8bbe\u8ba1\u6a21\u5f0f\u548c\u8fd0\u7528.<\/p>\n\n\n\n<p>\u6211\u521a\u5f00\u59cb\u63a5\u89e6symfony\u7684\u65f6\u5019\uff0c\u89c9\u5f97entity, repo\u7c7b\u4f3czend framework\u91cc\u9762\u7684model\u548cmapper\u7684\u5173\u7cfb\u3002 model\u662f\u6570\u636e\u6a21\u578b\uff0c mapper\u662f\u64cd\u4f5c\u6570\u636e\u6a21\u578b\u3002  \u663e\u7136\u8fd9\u79cd\u7406\u89e3\u662f\u80a4\u6d45\u7684\u3002<\/p>\n\n\n\n<p>Entity\u548cRepo\u7684\u6982\u5ff5\u662f\u5728DDD\u91cc\u9762\u63d0\u51fa\u6765\u7684\u3002\u8be6\u7ec6\u7684\u8bf4\u660e\u63a8\u8350\u9605\u8bfb\uff1a<a href=\"https:\/\/developer.aliyun.com\/article\/758292\">\u6bb7\u6d69\u8be6\u89e3DDD\u7cfb\u5217 \u7b2c\u4e09\u8bb2 &#8211; Repository\u6a21\u5f0f<\/a>\u3002 \u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u5256\u6790Repo\u7684\u672c\u8d28\u6bd4\u8f83\u900f\u5f7b\u3002\u4ed6\u7528\u7684\u662fJava\u4e16\u754c\u7684\u5b9e\u73b0\u3002\u5728PHP\u4e16\u754c\uff0c\u7279\u522b\u662fSymfon\u6846\u67b6\u5185\uff0c\u4e0e\u4e4b\u76f8\u6bd4\u53c8\u6709\u4e00\u4e9b\u6bd4\u8f83\u7279\u6b8a\u7684\u5730\u65b9\u3002<\/p>\n\n\n\n<p>Repo\u662f\u4e00\u79cd\u89c4\u8303\uff0c\u7528\u6765\u9694\u79bbEntity(\u5b9e\u4f53\uff0c\u9886\u57df\u5185\u7684\u6a21\u578b)\u548cDO\uff08\u6570\u636e\u6a21\u578b\uff0c\u6301\u4e45\u5c42\u7684\u6570\u636e\u6a21\u578b\uff09\u3002<\/p>\n\n\n\n<p>1. Repo\u7684\u4f7f\u7528\u201c\u4e2d\u6027\u201d\u7684\u52a8\u8bcd\uff0c\u800c\u4e0d\u8be5\u4f7f\u7528\u6570\u636e\u6301\u4e45\u5c42\u7684\u4e13\u7528\u52a8\u8bcd, \u6bd4\u5982\u907f\u514d\u4f7f\u7528 select, insert, , update, delete\u8fd9\u6837\u7684\u52a8\u8bcd\u4f5c\u4e3a\u65b9\u6cd5\u540d\u3002<\/p>\n\n\n\n<p>2. Repo\u662f\u670d\u52a1\u4e8eEntity\u7684\uff0c\u540c\u65f6\u81ea\u8eab\u4e0d\u8be5\u76f4\u63a5\u64cd\u4f5cDO\uff08\u6bd4\u5982\u6570\u636e\u5e93\uff09\u3002 \u6240\u4ee5\u63a8\u8350\u52a8\u8bcd<code>find<\/code>\u3001<code>save<\/code>\u3001<code>remove<\/code> \u6765\u4f5c\u4e3a\u65b9\u6cd5\u540d<\/p>\n\n\n\n<p>\u5728\u63a5\u89e6\u8fd9\u4e9b\u4fe1\u606f\u4e4b\u524d\uff0c\u6211\u66fe\u7ecf\u5728DH\u9879\u76ee\u91cc\u81ea\u5df1\u5b9e\u73b0\u8fc7\u5bf9DDD\u7684\u7406\u89e3\u3002\u5f53\u65f6\u6211\u7406\u89e3\u7684\u9886\u57df\u5185\u7684\u6a21\u578b\uff0c\u5c31\u7c7b\u4f3c\u4e00\u4e2a\u9700\u8981\u540c\u65f6\u4fee\u6539\u591a\u4e2a\u6570\u636e\u8868\u7684\u6a21\u578b\u3002<\/p>\n\n\n\n<p>\u5728<a href=\"https:\/\/www.thinktocode.com\/2018\/03\/05\/repository-pattern-symfony\/\">\u8fd9\u7bc7\u6587\u7ae0\u4e2d<\/a>, \u4f5c\u8005\u5728symfony\u6846\u67b6\u4e2d\u5982\u4f55\u4f7f\u7528Repo \u53d1\u8868\u4e86\u89c1\u89e3\u3002 \u9996\u5148\uff0c\u4ed6\u5426\u5b9a\u4e86symfony\u6587\u6863\u4e2d\u7684\u8303\u4f8b\u3002\u5728\u8fd9\u4e2a\u8303\u4f8b\u4e2d\uff0c\u6587\u6863\u4f5c\u8005\u5728controller\u91cc\u9762\u901a\u8fc7\u7ee7\u627f\u83b7\u5f97\u7684doctrie\u5bf9\u8c61\u6765\u83b7\u53d6entity manager\uff0c \u7136\u540e\u7528\u5b83\u521b\u5efaRepo\u5bf9\u8c61\u3002\u4f5c\u8005\u8bf4\u53ea\u6709\u521d\u5b66\u8005\u624d\u4f1a\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\uff0c\u56e0\u4e3a\u4f9d\u8d56\u88ab\u5199\u6b7b\u4e86\u3002\u4ed6\u5efa\u8bae\u4f7f\u7528service. <\/p>\n\n\n\n<p>\u5199\u4e00\u4e2arepo service, \u8ba9controller\u53ea\u4f9d\u8d56\u8fd9\u4e2aservice. \u5728\u8fd9\u4e2aservice\u91cc\uff0c\u901a\u8fc7\u6ce8\u5165repo\u5bf9\u8c61\u3002\u7136\u540erepo\u5bf9\u8c61\u4e5f<strong>\u4e0d\u8981\u4f7f\u7528\u7ee7\u627f<\/strong>\u83b7\u5f97\u7684findAll\u7b49\u65b9\u6cd5\uff0c\u800c<strong>\u53ea\u4f7f\u7528\u81ea\u5bb6\u7684\u65b9\u6cd5<\/strong>\u3002 \u4f5c\u8005\u79f0\u4e3a&#8221;use composition instead of inheritance&#8221;. \u6240\u4ee5\u9700\u8981\u5199\u4e00\u4e2arepo\u7684interface\uff0c\u7136\u540e\u628a\u81ea\u5bb6\u7684\u65b9\u6cd5\u90fd\u5728\u8fd9\u4e2ainterface\u5185\u5b9a\u4e49\u597d\u3002\u8fd9\u6837\u5199\u7684\u597d\u5904\u662f\uff0c\u8131\u8026\u3002<\/p>\n\n\n\n<p>\u7ed3\u679c\u8c03\u7528\u5173\u7cfb\u5c31\u4ece <\/p>\n\n\n\n<p><code>controller -&gt; doctrine -&gt; manager -&gt; repo -&gt; doctrine <\/code><\/p>\n\n\n\n<p>\u53d8\u4e3a\u4e86<\/p>\n\n\n\n<p><code>controller-&gt; service -&gt; repo -&gt; doctrine<\/code><\/p>\n\n\n\n<p>\u8fd9\u6837\u7684\u5199\u7684\u597d\u5904\u5c31\u662f\u5206\u5c42\u4e86\u3002<\/p>\n\n\n\n<p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u5185\uff0c\u4e0d\u76f4\u63a5\u4f7f\u7528doctrine\uff0c\u800c\u4f7f\u7528service\uff0c\u79f0\u4e3a\u653e\u5f03 Service Locator Pattern&nbsp;( \u8bbe\u8ba1\u6a21\u5f0f)\u800c\u662f\u4f7f\u7528 Dependcy injection (\u4f9d\u8d56\u6ce8\u5165\u6a21\u5f0f)<\/p>\n\n\n\n<p>\u4e0d\u76f4\u63a5\u4f7f\u7528\u7ee7\u627f\u83b7\u5f97\u7684\u65b9\u6cd5\uff0c\u800c\u662f\u8c03\u7528\u81ea\u5df1\u7684\u65b9\u6cd5\u53eb\u505a\u907f\u514d\u8fdd\u53cd Low of demeter \u8fea\u7c73\u7279\u6cd5\u5219 (<a href=\"https:\/\/www.jianshu.com\/p\/4b244f132439\">\u4ecb\u7ecd<\/a>)<\/p>\n\n\n\n<p>\u5206\u5c42\u597d\u5904\u7684\u4f53\u73b0\uff1a<\/p>\n\n\n\n<p><strong>\u6d4b\u8bd5\u5bb9\u6613\u4e86<\/strong>\u3002\u7531\u4e8e\u6bcf\u4e00\u5c42\u53ea\u8c03\u7528\u81ea\u5df1\u7684\u4e0b\u4e00\u5c42\uff0c\u6240\u4ee5mock\u8d77\u6765\u7279\u522b\u65b9\u4fbf\u3002 \u8981\u6d4b\u8bd5conterller\u53ea\u9700\u8981\u6a21\u62df\u4e00\u4e2aservice. \u8981\u6d4b\u8bd5service\u53ea\u9700\u8981\u6a21\u62df\u4e00\u4e2arepo. \u6d4b\u8bd5repo\u53ea\u9700\u8981\u6a21\u62df\u4e00\u4e2adoctrine\u3002\u907f\u514d\u4e86\u590d\u6742\u7684\u4f9d\u8d56\u5173\u7cfb<\/p>\n\n\n\n<p><strong>\u6269\u5c55\u5bb9\u6613\u4e86<\/strong>\u3002\u6bd4\u5982\u5982\u679c\u9700\u8981\u5728\u6bcf\u6b21\u4fdd\u5b58\u5bf9\u8c61\u540e\u89e6\u53d1\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u90a3\u4e48\u53ea\u8981\u6269\u5c55service\u5c42\u5c31\u53ef\u4ee5\u4e86\u3002\u8fd9\u6837controller\u53ef\u4ee5\u4e00\u76f4\u4fdd\u6301\u82d7\u6761\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong><a href=\"https:\/\/www.thinktocode.com\/2018\/01\/08\/repository-pattern\/\">Repository Pattern<\/a><\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/www.thinktocode.com\/2018\/03\/05\/repository-pattern-symfony\/\">Repository Pattern in Symfony<\/a><\/p>\n\n\n\n<p><strong><a href=\"https:\/\/www.thinktocode.com\/2019\/01\/24\/doctrine-repositories-should-be-collections-without-flush\/\">Doctrine Repositories should be collections without flush<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e09\u7bc7\u6587\u7ae0\u5e2e\u4f60\u7406\u89e3Repository\u8bbe\u8ba1\u6a21\u5f0f\u548c\u8fd0\u7528. \u6211\u521a\u5f00\u59cb\u63a5\u89e6symfony &hellip; <a href=\"https:\/\/chen-hongyi.com\/?p=686\">\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":[],"class_list":["post-686","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/posts\/686","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=686"}],"version-history":[{"count":6,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/posts\/686\/revisions"}],"predecessor-version":[{"id":695,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=\/wp\/v2\/posts\/686\/revisions\/695"}],"wp:attachment":[{"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chen-hongyi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}