By this implementation, User struct from entity.go is a domain/business entity. So it should know nothing about json(API), bson(Persistance) or any other infrastructure.
Such Golang tags make the domain entity dependent from infrastructure. And it is an insidious dependency: linter can't easly detect it on syntax layer (no imports) and should understand semantic of tags information.
So, I recommend to avoid any tags in domain layer. Instead use DTOs and mappings in infrastructure:
http.UserJSON, or even http.UserViewJSON, http.UserCreateJSON with json tags
Yes! Actually, this is a correct behavior and I’m using something like this in my codes. I was thinking in an update to this post, but your comment cover it :) thanks
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
By this implementation,
User
struct fromentity.go
is a domain/business entity. So it should know nothing aboutjson
(API),bson
(Persistance) or any other infrastructure.Such Golang tags make the domain entity dependent from infrastructure. And it is an insidious dependency: linter can't easly detect it on syntax layer (no imports) and should understand semantic of tags information.
So, I recommend to avoid any tags in domain layer. Instead use DTOs and mappings in infrastructure:
http.UserJSON
, or evenhttp.UserViewJSON
,http.UserCreateJSON
withjson
tagsmongodb.UserDoc
withbson
tagshttp.UserJSON
->domain.User
<-mongodb.UserDoc
Thanks for the article. Ready for discussion.
Yes! Actually, this is a correct behavior and I’m using something like this in my codes. I was thinking in an update to this post, but your comment cover it :) thanks