英文:
golang domain driven design entities including json attributes in structs
问题
我正在使用DDD来设计我的Go应用程序。在实体层中,我将使用structs
,它看起来像这样:
type FruitBasket struct {
Id int64 `json:"ref"`
private string `json:"ttl"`
}
在DDD中,将json
属性包含在这个struct
中作为实体层是否可以?我更倾向于不这样做,但不确定。是否有其他替代方案我应该考虑?
英文:
I am using DDD to design my go application. In the entity layer, I will be using structs
and it looks like this
type FruitBasket struct {
Id int64 `json:"ref"`
private string `json:"ttl"`
}
Is it ok to include the json
attributes in this struct
which forms the entity layer in the DDD
? I would prefer not to do it, but not sure. Is there any other alternative which I should consider?
答案1
得分: 1
通常,JSON 标签用于在 HTTP API 中取消编组请求并编组响应。
更具动态性的一种方法是使用特定的请求和响应结构,就像这个例子一样,我从 Mat Ryers 的文章《八年后,我如何编写 HTTP 服务》中找到的。
func (s *server) handleSomething() http.HandlerFunc {
type request struct {
Name string
}
type response struct {
Greeting string `json:"greeting"`
}
return func(w http.ResponseWriter, r *http.Request) {
...
}
}
英文:
Usually the JSON tags are used to unmarshal requests and marshal responses in HTTP APIs.
Something that gives you more dynamicity is having specific request and response structures like this example, which I took from Mat Ryers post of
How I write HTTP services after eight years.
func (s *server) handleSomething() http.HandlerFunc {
type request struct {
Name string
}
type response struct {
Greeting string `json:"greeting"`
}
return func(w http.ResponseWriter, r *http.Request) {
...
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论