美文网首页
聊聊go-ddd-sample

聊聊go-ddd-sample

作者: go4it | 来源:发表于2021-03-23 00:00 被阅读0次

本文主要赏析一下go-ddd-sample

项目结构

├── _sql
├── application
├── config
├── domain
│   └── repository
├── infrastructure
│   └── persistence
│       └── testdata
└── interfaces
    └── testdata

这里分为application、domain、infrastructure、interfaces四层

domain

├── repository
│   ├── mock_user.go
│   └── user.go
└── user.go

domain层定义了模型及repository接口,同时利用go generate生成repository的mock实现

application

// UserInteractor provides use-case
type UserInteractor struct {
    Repository repository.UserRepository
}

// GetUser returns user
func (i UserInteractor) GetUser(ctx context.Context, id int) (*domain.User, error) {
    return i.Repository.Get(ctx, id)
}

// GetUsers returns user list
func (i UserInteractor) GetUsers(ctx context.Context) ([]*domain.User, error) {
    return i.Repository.GetAll(ctx)
}

// AddUser saves new user
func (i UserInteractor) AddUser(ctx context.Context, name string) error {
    u, err := domain.NewUser(name)
    if err != nil {
        return err
    }
    return i.Repository.Save(ctx, u)
}

applicatioin层调用domain层来进行业务编排

infrastructure

└── persistence
    ├── main_test.go
    ├── testdata
    │   ├── schema.sql -> ../../../_sql/schema.sql
    │   └── users.yml
    ├── user_repository.go
    └── user_repository_test.go

infrastructure的persistence实现了domain层定义的repository接口

interfaces

├── handler.go
├── handler_test.go
├── main_test.go
└── testdata
    ├── schema.sql -> ../../_sql/schema.sql
    └── users.yml

interfaces基于net/http来提供http接口

小结

go-ddd-sample分为application、domain、infrastructure、interfaces四层,其中domain定义repository接口,infrastructure层实现该接口,application层通过domain来编排业务逻辑,interfaces层则基于net/http来提供http接口。

doc

相关文章

  • 聊聊go-ddd-sample

    序 本文主要赏析一下go-ddd-sample 项目结构 这里分为application、domain、infra...

  • 聊聊…聊聊?

    世界不大,一座城市里,用高楼大厦圈出来的的圈子更小了… 心再大,也会被城市里喧嚣的汽笛压抑自己 不记得有多久没有好...

  • 聊聊聊

    今天主要的时间是和阿q过的,非常开心我们有了这么一次聊天! 我觉得自己不孤单了。我俩目前拥有的感情非常相似,是比较...

  • 无聊聊聊

  • 聊聊,聊聊选择

    今早梦到一杯豆浆15元,我给自己的孩子买了一杯50元的奶茶,对她感叹“在我们那个年代一杯奶茶才10元”孩子问我那么...

  • 聊聊,聊聊闲时

    有段时间着了迷一样的看伍迪艾伦电影,印象最深的就是电影开场他一张大脸挤满了屏幕,絮絮叨叨两分钟,正片开始。 后来得...

  • 37

    今晚不想你睡 想和你聊聊聊聊聊到天天天天天长地久

  • 那个我以为很酷的男生

    熄了 灯,朋友打电话来聊天,聊了好久好久… 聊聊过去,聊聊现在,聊聊未来,聊聊别人,也聊聊自己… 一晃我们过了二字...

  • 悠然自得——二舅家游记(下)

    我们一起聊聊工作,聊聊生活,聊聊城市,聊聊乡村,聊聊猪场,聊聊门前那条黑背。我不争气的扒在窗口,安安静静地看着它。...

  • 我一直不缺母爱20210106

    元旦,儿子放月假。 其实我儿子每次放假回家都会和我聊聊天,聊聊学校,聊聊老师,聊聊同学,聊聊他自己,没有着重点,都...

网友评论

      本文标题:聊聊go-ddd-sample

      本文链接:https://www.haomeiwen.com/subject/jrlxhltx.html