聊聊go-ddd-sample

codecraft · · 358 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

本文主要赏析一下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


有疑问加站长微信联系(非本文作者)

本文来自:Segmentfault

感谢作者:codecraft

查看原文:聊聊go-ddd-sample

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

358 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传