前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊go-ddd-sample

聊聊go-ddd-sample

作者头像
code4it
发布2021-03-24 17:24:49
3600
发布2021-03-24 17:24:49
举报
文章被收录于专栏:码匠的流水账码匠的流水账

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

项目结构

代码语言:javascript
复制
├── _sql
├── application
├── config
├── domain
│   └── repository
├── infrastructure
│   └── persistence
│       └── testdata
└── interfaces
    └── testdata

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

domain

代码语言:javascript
复制
├── repository
│   ├── mock_user.go
│   └── user.go
└── user.go

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

application

代码语言:javascript
复制
// 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

代码语言:javascript
复制
└── persistence
    ├── main_test.go
    ├── testdata
    │   ├── schema.sql -> ../../../_sql/schema.sql
    │   └── users.yml
    ├── user_repository.go
    └── user_repository_test.go

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

interfaces

代码语言:javascript
复制
├── 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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-03-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 项目结构
  • domain
  • application
  • infrastructure
  • interfaces
  • 小结
  • doc
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档