From 6cb3b5db202bf97bf438d13cac7fb2eae56f5639 Mon Sep 17 00:00:00 2001 From: jacexh Date: Sun, 5 Jul 2020 15:47:21 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=8C=BA=E5=88=86do=E3=80=81entity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 ++++++- application/user.go | 8 ++++---- cmd/main.go | 6 +++--- domain/user/user.go | 11 ++++------- .../{repository => persistence}/database.go | 2 +- infrastructure/{repository => persistence}/user.go | 12 +++++++++++- types/do/do.go | 13 +++++++++++++ types/dto.go | 10 ---------- types/dto/dto.go | 13 +++++++++++++ 9 files changed, 55 insertions(+), 27 deletions(-) rename infrastructure/{repository => persistence}/database.go (97%) rename infrastructure/{repository => persistence}/user.go (63%) create mode 100644 types/do/do.go delete mode 100644 types/dto.go create mode 100644 types/dto/dto.go diff --git a/README.md b/README.md index 8c83e0d..2d8baae 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,11 @@ a DDD project template in golang ## Change Log +### 0.2.1 + +- 区分`DataObject`以及`Entity` +- 修改目录名称 `infrastructure/repository` -> `infrastructure/persistence` + ### 0.2.0 基于依赖反转原则以及六边形架构重构整个项目 @@ -19,4 +24,4 @@ a DDD project template in golang - `Repository`定义在`Domain`层内 - `Application`+`Domain` 使用依赖反转,具备了更好的可测试性 - 更清晰的分支管理:`master`分支为golang项目,`template`分支为模板 -- 严格区分了`Entity`、`ValueObject`、`DataTransferObject`、`DomainEvent`等 \ No newline at end of file +- 严格区分了`Entity`、`ValueObject`、`DataTransferObject`、`DomainEvent`等 diff --git a/application/user.go b/application/user.go index 411e6ca..90f06eb 100644 --- a/application/user.go +++ b/application/user.go @@ -4,7 +4,7 @@ import ( "context" "github.com/jacexh/golang-ddd-template/domain/user" - "github.com/jacexh/golang-ddd-template/types" + "github.com/jacexh/golang-ddd-template/types/dto" ) var ( @@ -23,8 +23,8 @@ func BuildUserApplication(repo user.UserRepository) { } } -func convert(user *user.UserEntity) *types.UserDTO { - return &types.UserDTO{ +func convert(user *user.UserEntity) *dto.UserDTO { + return &dto.UserDTO{ ID: user.ID, Name: user.Name, Email: user.Email, @@ -35,7 +35,7 @@ func (ua *userApplication) WithUserRepository(repo user.UserRepository) { ua.repo = repo } -func (ua *userApplication) GetUserByID(ctx context.Context, uid string) (*types.UserDTO, error) { +func (ua *userApplication) GetUserByID(ctx context.Context, uid string) (*dto.UserDTO, error) { user, err := ua.repo.GetUserByID(ctx, uid) if err != nil { return nil, err diff --git a/cmd/main.go b/cmd/main.go index 0fb0952..5b7c8cb 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -13,7 +13,7 @@ import ( "syscall" "github.com/jacexh/golang-ddd-template/application" - "github.com/jacexh/golang-ddd-template/infrastructure/repository" + "github.com/jacexh/golang-ddd-template/infrastructure/persistence" "github.com/jacexh/golang-ddd-template/logger" "github.com/jacexh/golang-ddd-template/option" "github.com/jacexh/golang-ddd-template/router" @@ -85,11 +85,11 @@ func main() { logger.Logger.Info("loaded options", zap.Any("option", opt), zap.String("version", version)) // 创建数据库连接 - db, err := repository.BuildDBConnection(opt.Database) + db, err := persistence.BuildDBConnection(opt.Database) if err != nil { logger.Logger.Panic("failed to connect with database", zap.Error(err)) } - ur := repository.NewUserRepository(db) + ur := persistence.NewUserRepository(db) // 初始化application层 application.BuildUserApplication(ur) diff --git a/domain/user/user.go b/domain/user/user.go index 1eb4cc3..556ee4a 100644 --- a/domain/user/user.go +++ b/domain/user/user.go @@ -1,11 +1,8 @@ package user -import "time" - type UserEntity struct { - ID string `ddb:"id"` - Name string `ddb:"name"` - Email string `ddb:"email"` - CTime time.Time `ddb:"ctime"` - MTime time.Time `ddb:"mtime"` + ID string + Name string + Password string + Email string } diff --git a/infrastructure/repository/database.go b/infrastructure/persistence/database.go similarity index 97% rename from infrastructure/repository/database.go rename to infrastructure/persistence/database.go index ba89ad3..88c8c61 100644 --- a/infrastructure/repository/database.go +++ b/infrastructure/persistence/database.go @@ -1,4 +1,4 @@ -package repository +package persistence import ( "database/sql" diff --git a/infrastructure/repository/user.go b/infrastructure/persistence/user.go similarity index 63% rename from infrastructure/repository/user.go rename to infrastructure/persistence/user.go index 8d61a1a..d509951 100644 --- a/infrastructure/repository/user.go +++ b/infrastructure/persistence/user.go @@ -1,10 +1,11 @@ -package repository +package persistence import ( "context" "database/sql" "github.com/jacexh/golang-ddd-template/domain/user" + "github.com/jacexh/golang-ddd-template/types/do" ) type ( @@ -13,6 +14,15 @@ type ( } ) +func convert(entity *user.UserEntity) *do.UserDo { + return &do.UserDo{ + ID: entity.ID, + Name: entity.Name, + Password: entity.Password, + Email: entity.Email, + } +} + func NewUserRepository(db *sql.DB) user.UserRepository { return &UserRepository{db} } diff --git a/types/do/do.go b/types/do/do.go new file mode 100644 index 0000000..02c6fbd --- /dev/null +++ b/types/do/do.go @@ -0,0 +1,13 @@ +package do + +import "time" + +type UserDo struct { + ID string `ddb:"id"` + Name string `ddb:"name"` + Password string `ddb:"password"` + Email string `ddb:"email"` + Version int `ddb:"version"` + CTime time.Time `ddb:"ctime"` + MTime time.Time `ddb:"mtime"` +} diff --git a/types/dto.go b/types/dto.go deleted file mode 100644 index e69d903..0000000 --- a/types/dto.go +++ /dev/null @@ -1,10 +0,0 @@ -package types - -type ( - // UserDTO use的dto定义,用于api层 - UserDTO struct { - ID string `json:"id"` - Name string `json:"name"` - Email string `json:"email"` - } -) diff --git a/types/dto/dto.go b/types/dto/dto.go new file mode 100644 index 0000000..4d575b4 --- /dev/null +++ b/types/dto/dto.go @@ -0,0 +1,13 @@ +package dto + +type ( + // UserDTO use的dto定义,用于api层 + UserDTO struct { + ID string `json:"id"` + Name string `json:"name"` + Email string `json:"email"` + Version string `json:"version"` + CTime string `json:"ctime"` + MTime string `json:"mtime"` + } +) From 3e8e4f57ea51a57d41ee6d985d0b02974162a2bc Mon Sep 17 00:00:00 2001 From: jacexh Date: Sun, 5 Jul 2020 15:54:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 4 ++-- application/user.go | 4 ++-- cmd/main.go | 10 +++++----- go.mod | 2 +- infrastructure/persistence/database.go | 2 +- infrastructure/persistence/user.go | 4 ++-- logger/logger.go | 2 +- router/router.go | 6 +++--- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Dockerfile b/Dockerfile index df880d4..909dc17 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,11 +9,11 @@ RUN set -e \ && apt update -y \ && apt install -y git \ && REVISION=`git rev-list -1 HEAD` \ - && go build -ldflags "-X main.version=$REVISION" -o app -tags=jsoniter cmd/main.go + && go build -ldflags "-X main.version=$REVISION" -o {{.BinFile}} -tags=jsoniter cmd/main.go FROM debian:buster WORKDIR /app COPY --from=builder /go/src/app . COPY --from=builder /go/src/cmd/config.yml . EXPOSE 8080 -CMD ["/app/app"] \ No newline at end of file +CMD ["/app/{{.BinFile}}"] \ No newline at end of file diff --git a/application/user.go b/application/user.go index 90f06eb..bfcd3df 100644 --- a/application/user.go +++ b/application/user.go @@ -3,8 +3,8 @@ package application import ( "context" - "github.com/jacexh/golang-ddd-template/domain/user" - "github.com/jacexh/golang-ddd-template/types/dto" + "{{.Module}}/domain/user" + "{{.Module}}/types/dto" ) var ( diff --git a/cmd/main.go b/cmd/main.go index 5b7c8cb..f38fc66 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -12,11 +12,11 @@ import ( "strings" "syscall" - "github.com/jacexh/golang-ddd-template/application" - "github.com/jacexh/golang-ddd-template/infrastructure/persistence" - "github.com/jacexh/golang-ddd-template/logger" - "github.com/jacexh/golang-ddd-template/option" - "github.com/jacexh/golang-ddd-template/router" + "{{.Module}}/application" + "{{.Module}}/infrastructure/persistence" + "{{.Module}}/logger" + "{{.Module}}/option" + "{{.Module}}/router" "github.com/jacexh/multiconfig" "go.uber.org/zap" ) diff --git a/go.mod b/go.mod index 57ac39d..4c16687 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/jacexh/golang-ddd-template +module {{.Module}} go 1.14 diff --git a/infrastructure/persistence/database.go b/infrastructure/persistence/database.go index 88c8c61..4877e4e 100644 --- a/infrastructure/persistence/database.go +++ b/infrastructure/persistence/database.go @@ -5,7 +5,7 @@ import ( "github.com/didi/gendry/manager" _ "github.com/go-sql-driver/mysql" - "github.com/jacexh/golang-ddd-template/option" + "{{.Module}}/option" ) var ( diff --git a/infrastructure/persistence/user.go b/infrastructure/persistence/user.go index d509951..8bc4689 100644 --- a/infrastructure/persistence/user.go +++ b/infrastructure/persistence/user.go @@ -4,8 +4,8 @@ import ( "context" "database/sql" - "github.com/jacexh/golang-ddd-template/domain/user" - "github.com/jacexh/golang-ddd-template/types/do" + "{{.Module}}/domain/user" + "{{.Module}}/types/do" ) type ( diff --git a/logger/logger.go b/logger/logger.go index d8beb70..e259e45 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -1,7 +1,7 @@ package logger import ( - "github.com/jacexh/golang-ddd-template/option" + "{{.Module}}/option" "github.com/jacexh/goutil/zaphelper" "go.uber.org/zap" "go.uber.org/zap/zapcore" diff --git a/router/router.go b/router/router.go index 21aab4b..70b7a82 100644 --- a/router/router.go +++ b/router/router.go @@ -5,9 +5,9 @@ import ( "net/http" "github.com/gin-gonic/gin" - "github.com/jacexh/golang-ddd-template/application" - "github.com/jacexh/golang-ddd-template/logger" - "github.com/jacexh/golang-ddd-template/option" + "{{.Module}}/application" + "{{.Module}}/logger" + "{{.Module}}/option" "github.com/jacexh/goutil/gin-middleware/ginzap" )