{"id":427282,"date":"2021-04-15T10:54:08","date_gmt":"2021-04-15T02:54:08","guid":{"rendered":"http:\/\/4563.org\/?p=427282"},"modified":"2021-04-15T10:54:08","modified_gmt":"2021-04-15T02:54:08","slug":"%e8%af%b7%e6%95%99%e4%b8%80%e4%b8%aa-golang-ddd-%e7%9a%84%e6%95%b0%e6%8d%ae%e8%bd%ac%e5%8c%96%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=427282","title":{"rendered":"\u8bf7\u6559\u4e00\u4e2a golang DDD \u7684\u6570\u636e\u8f6c\u5316\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u6559\u4e00\u4e2a golang DDD \u7684\u6570\u636e\u8f6c\u5316\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : shawncheung <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u76ee\u524d\u60f3\u8981\u5728\u670d\u52a1\u91cc\u9762\u843d\u5730 DDD\uff0cDDD \u91cc\u9762\u63a8\u8350\u4f7f\u7528 Entity \u548c Value Object \u5982\u8fd9\u7bc7\u6587\u7ae0\u6240\u8bf4\uff1a https:\/\/www.damianopetrungaro.com\/posts\/ddd-using-golang-tactical-design\/<\/p>\n<p>\u4f46\u662f\u5728\u843d\u5730\u7684\u65f6\u5019\u53d1\u73b0\u6570\u636e\u8f6c\u5316\u7684\u95ee\u9898\uff1a\u6bd4\u5982\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>Link<\/code> Value Object<\/p>\n<pre><code>type Link struct {  value string }  func (u Link) String() string {  return u.value }  func NewLink(s string) (URL, error) {  if match, _ := regexp.MatchString(`^( https?|ftp):\/\/[^s\/$.?#].[^s]*$`, s); !match {   return Link{}, fmt.Errorf(\"project url format is invalid: %v\", s)  }  return Link{value: s}, nil }   type Project struct {  ID uint64  Name string     Url Link } <\/code><\/pre>\n<p>\u7136\u540e\u6846\u67b6\u4f7f\u7528 grpc\uff0c\u90a3\u4e48 grpc \u672c\u8eab\u662f\u6709 pb.XXXRequest \u8fd9\u79cd\u7c7b\u578b\u7684\uff1a<\/p>\n<pre><code>func (s *ServerImpl) Create(  ctx context.Context,     req *pb.CreateRequest,     res *pb.CreateResponse, ) error {  url, err := model.NewLink(req.URL)     if err != nil {      return err     }     \/\/...\u5982\u679c\u8fd9\u91cc\u6709\u5f88\u591a\u90fd\u662f\u8fd9\u79cd\u503c\u5bf9\u8c61\u7684\u5b57\u6bb5\uff0c\u5728\u8fd9\u91cc\u5c82\u4e0d\u662f\u5f88\u591a\u5f88\u591a\u7c7b\u4f3c     \/\/ if err != nil { ... } \u8fd9\u6837\u7684\u4ee3\u7801\u5417\uff1f\u800c\u4e14\u8fd9\u6837\u624b\u5de5\u8f6c\u4e5f\u6bd4\u8f83\u9ebb\u70e6  project := model.Project{      ID: req.ID,         Url: url     } } <\/code><\/pre>\n<p>\u5404\u4f4d\u9047\u5230\u8fd9\u6837\u7684\u95ee\u9898\u6709\u6ca1\u6709\u4ec0\u4e48\u6bd4\u8f83\u597d\u7684\u6bd4\u8f83\u4f18\u96c5\u7684\u65b9\u5f0f\u6765\u89e3\u51b3\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>2<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5736089\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : baiyi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u91cc\u7684\u4e3b\u8981\u95ee\u9898\u5e94\u8be5\u662f\u5c06\u5b57\u6bb5\u7684\u6821\u9a8c\u653e\u5728\u4e86\u503c\u5bf9\u8c61\u7684 New \u65b9\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u628a\u5b83\u5265\u79bb\u51fa\u6765\uff0c\u50cf\u73b0\u6709\u7684\u4e00\u4e9b\u5e93\u4e00\u6837\uff0c\u628a\u89c4\u5219\u5199\u5728 tag \u4e0a\uff0c\u7136\u540e\u7edf\u4e00\u9a8c\u8bc1\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5736090\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : shawncheung <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @baiyi \u8fd9\u91cc\u4e0d\u6b62\u662f\u6821\u9a8c\u7684\u95ee\u9898\uff0c\u8fd8\u53ef\u80fd\u6709\u4e00\u4e9b\u4e1a\u52a1\u903b\u8f91\uff0c\u5c31\u662f\u60f3\u95ee\u4e0b value object \u5728 golang \u91cc\u9762\u600e\u4e48\u5904\u7406\uff0c\u5199\u7684\u4f18\u96c5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6559\u4e00\u4e2a golang DDD \u7684&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/427282"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=427282"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/427282\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=427282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=427282"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=427282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}