Rich domain model in golang

Written by
Link to Post by 

“` type Article struct { // entity model ID uint64 // some fields … ArticleRepo ArticleRepository AuthorRepo AuthorRepository }

type ArticleRepository interface { Get(id uint64) (…) }

type ArticleModel struct { // data model for mysql ID uint64 sql:"..." } “` Here is a Article entity, which use ArticleRepository interface to save itself and use AuthorRepository to find a Author entity. My problem is ArticleRepository query a article from mysql and get a data model, how do i rebuild it into a entity(inject repository)?

“` type ArticleFactory struct { ArticleRepo AuthorRepo }

func (f *ArticleFactory) Gen() *Article { return &Article{ ArticleRepo: f.ArticleRepo, AuthorRepo: f.AuthorRepo, } } “` if i use a Article to ArticleFactory to generate a entity in ArticleRepository.Get, the ArticleRepository is dependent on ArticleFactory, and the ArticleFactory is dependent on ArticleRepository, how to solve the circle dependency?

submitted by /u/liguangsheng
[link] [comments]

Article Tags:
· ·
Article Categories:

Leave a Reply