这才是java整洁架构的正确之路
引读:就在上周日,我在 GitHub 闲逛(就像我的大部分周日一样),偶然发现了一个非常受欢迎超过 10K 的提交量的仓库,我不打算说出名字。尽管我知道这个项目的技术栈,但对其代码还不太熟悉。里面不少功能被随机地扔在了一个名为 utils 或更糟糕的 helpers 目录下面。
大项目的陷阱是,随着时间的推移,它们会变得非常复杂,以至于重写比培养新人来理解代码然后修改要容易得多。
这使我想到了从实现层面谈整洁架构。这篇文章将包含一些 Go 代码,但不用担心,即使你不熟悉这门语言,要说的概念也是相当容易理解的。
什么是整洁架构?
简而言之,你会从使用整洁架构中获得以下好处。
-
数据库无关性:核心业务逻辑不用关心使用 Postgres、MongoDB 还是 Neo4J。
-
客户端接口无关性:核心业务逻辑不关心你是否使用 CLI、REST API,甚至是 gRPC。
-
框架无关性:使用 vanilla nodeJS、express、fastify?你的核心业务逻辑也不关心这些。
现在,如果你想更多了解整洁架构是如何工作的,你可以阅读 Bob 大叔的博客 (2)。现在,让我们展开一个整洁架构的示例实现,GitHub 可参看 (1)。
Clean-Architecture-Sample
├── api
│ ├── handler
│ │ ├── admin.go
│ │ └── user.go
│ ├── main.go
│ ├── middleware
│ │ ├── auth.go
│ │ └── cors.go
│ └── views
│ └── errors.go
├── bin
│ └── main
├── config.json
├── docker-compose.yml
├── go.mod
├── go.sum
├── Makefile
├── pkg
│ ├── admin
│ │ ├── entity.go
│ │ ├── postgres.go
│ │ ├── repository.go
│ │ └── service.go
│ ├── errors.go
│ └── user
│ ├── entity.go
│ ├── postgres.go
│ ├── repository.go
│ └── service.go
├── README.md
实体
实体是可以通过函数实现的核心业务对象。用 MVC 术语来说,它们是整洁架构的模型层。所有的实体和服务都封装在 pkg 目录中。这其实就是我们要抽象出的东西,让它和其他部分分开。
如果你看一下 user 下面的 entity.go ,它看起来是这样的
package user
import "github.com/jinzhu/gorm"
type User struct {
gorm.Model
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Password string `json:"password,omitempty"`
PhoneNumber string `json:"phone_number,omitempty"`
Email string `json:"email,omitempty"`
Address string `json:"address,omitempty"`
DisplayPic string `json:"display_pic,omitempty"`
}
实体是在 Repository 接口中使用的,它可以用任何数据库实现。在本例中,我们在 postgres.go 中用 Postgres 实现了它,由于 Repository 可以用任何数据库实现,因此与所实现细节无关。
package user
import (
"context"
)
type Repository interface {
FindByID(ctx context.Context, id uint) (*User, error)
BuildProfile(ctx context.Context, user *User) (*User, error)
CreateMinimal(ctx context.Context, email, password, phoneNumber string) (*User, error)
FindByEmailAndPassword(ctx context.Context, email, password string) (*User, error)
FindByEmail(ctx context.Context, email string) (*User, error)
DoesEmailExist(ctx context.Context, email string) (bool, error)
ChangePassword(ctx context.Context, email, password string) error
}
Service
服务包括面向更高层次的业务逻辑功能的接口。例如,FindByID 可能是一个存储层函数,但 login 或 signup 则是服务层函数。服务是存储的抽象层,它们不与数据库交互,而是与存储的接口交互。
package user
import (
"context"
"crypto/md5"
"encoding/hex"
"errors"
)
type Service interface {
Register(ctx context.Context, email, password, phoneNumber string) (*User, error)
Login(ctx context.Context, email, password string) (*User, error)
ChangePassword(ctx context.Context, email, password string) error
BuildProfile(ctx context.Context, user *User) (*User, error)
GetUserProfile(ctx context.Context, email string) (*User, error)
IsValid(user *User) (bool, error)
GetRepo() Repository
}
type service struct {
repo Repository
}
func NewService(r Repository) Service {
return &service{
repo: r,
}
}
func (s *service) Register