使用 go 来做 web 开发时,orm 可以在业务对象和数据库之间建立映射,但是没有找到类似 java 中 dto 类似功能的库,能够在表现层和业务对象之间做一层隔离。
例如我想查询所有的 candidate 并且以 json 的形式返回给前端,那么我必须这么做:
type Candidate struct {
Address string `bson:"address" json:"address"` // owner
PubKey string `bson:"pub_key" json:"pub_key"`
Shares int64 `bson:"shares" json:"shares"`
VotingPower uint64 `bson:"voting_power" json:"voting_power"` // Voting power if pubKey is a considered a validator
Description Description `bson:"description" json:"description"` // Description terms for the candidate
}
但是,这样子做就是将UI对象和业务对象耦合在了一起,在设计和开发上都有些不合理。所以,就想请问 go 语言中是否有类似 dto 的库来做一层分离?
这个隔离的话,自己在业务层加一个转换不就行了么?也不复杂