小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

整潔架構(gòu)的正確之路

 taotao_2016 2020-05-24

就在上周日,我在 GitHub 閑逛(就像我的大部分周日一樣),偶然發(fā)現(xiàn)了一個非常受歡迎超過 10K 的提交量的倉庫,我不打算說出名字。盡管我知道這個項目的技術(shù)棧,但對其代碼還不太熟悉。里面不少功能被隨機地扔在了一個名為 utils 或更糟糕的 helpers 目錄下面。

大項目的陷阱是,隨著時間的推移,它們會變得非常復雜,以至于重寫比培養(yǎng)新人來理解代碼然后修改要容易得多。

這使我想到了從實現(xiàn)層面談整潔架構(gòu)。這篇文章將包含一些 Go 代碼,但不用擔心,即使你不熟悉這門語言,要說的概念也是相當容易理解的。

什么是整潔架構(gòu)?

整潔架構(gòu)的正確之路

簡而言之,你會從使用整潔架構(gòu)中獲得以下好處。

  • 數(shù)據(jù)庫無關(guān)性:核心業(yè)務邏輯不用關(guān)心使用 Postgres、MongoDB 還是 Neo4J。

  • 客戶端接口無關(guān)性:核心業(yè)務邏輯不關(guān)心你是否使用 CLI、REST API,甚至是 gRPC。

  • 框架無關(guān)性:使用 vanilla nodeJS、express、fastify?你的核心業(yè)務邏輯也不關(guān)心這些。

現(xiàn)在,如果你想更多了解整潔架構(gòu)是如何工作的,你可以閱讀 Bob 大叔的博客 (2)?,F(xiàn)在,讓我們展開一個整潔架構(gòu)的示例實現(xiàn),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

實體

實體是可以通過函數(shù)實現(xiàn)的核心業(yè)務對象。用 MVC 術(shù)語來說,它們是整潔架構(gòu)的模型層。所有的實體和服務都封裝在 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'`}

pkg/user/entity.go

實體是在 Repository 接口中使用的,它可以用任何數(shù)據(jù)庫實現(xiàn)。在本例中,我們在 postgres.go 中用 Postgres 實現(xiàn)了它,由于 Repository 可以用任何數(shù)據(jù)庫實現(xiàn),因此與所實現(xiàn)細節(jié)無關(guān)。

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}

pkg/user/repository.go

Service

服務包括面向更高層次的業(yè)務邏輯功能的接口。例如,F(xiàn)indByID 可能是一個存儲層函數(shù),但 login 或 signup 則是服務層函數(shù)。服務是存儲的抽象層,它們不與數(shù)據(jù)庫交互,而是與存儲的接口交互。

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(ctx context.Context, email, password, phoneNumber string) (u *User, err error) {
exists, err := s.repo.DoesEmailExist(ctx, email) if err != nil { return nil, err } if exists { return nil, errors.New('User already exists') }
hasher := md5.New hasher.Write(byte(password))
return s.repo.CreateMinimal(ctx, email, hex.EncodeToString(hasher.Sum(nil)), phoneNumber)}
func (s *service) Login(ctx context.Context, email, password string) (u *User, err error) {
hasher := md5.New hasher.Write(byte(password)) return s.repo.FindByEmailAndPassword(ctx, email, hex.EncodeToString(hasher.Sum(nil)))}
func (s *service) ChangePassword(ctx context.Context, email, password string) (err error) {
hasher := md5.New hasher.Write(byte(password)) return s.repo.ChangePassword(ctx, email, hex.EncodeToString(hasher.Sum(nil)))}
func (s *service) BuildProfile(ctx context.Context, user *User) (u *User, err error) {
return s.repo.BuildProfile(ctx, user)}
func (s *service) GetUserProfile(ctx context.Context, email string) (u *User, err error) { return s.repo.FindByEmail(ctx, email)}
func (s *service) IsValid(user *User) (ok bool, err error) {
return ok, err}
func (s *service) GetRepo Repository {
return s.repo}

pkg/user/service.go

服務是在用戶接口層面實現(xiàn)的。

接口適配器

每個用戶接口都有獨立的目錄。在我們的例子中,因為我們用 API 作為接口,因此有一個叫 api 的目錄。

現(xiàn)在,由于每個用戶接口對請求的監(jiān)聽方式不同,所以接口適配器都有自己的 main.go 文件,其任務如下。

  • 創(chuàng)建 Repository

  • 在服務內(nèi)的包裝 repository

  • 在 Handler 里面包裝服務

在這里,Handler 程序只是 Request-Response 模型的用戶接口實現(xiàn)。每個服務都有自己的 Handler 程序。參見 user.go

package handler
import ( 'encoding/json' 'net/http'
'github.com/L04DB4L4NC3R/jobs-mhrd/api/middleware' 'github.com/L04DB4L4NC3R/jobs-mhrd/api/views' 'github.com/L04DB4L4NC3R/jobs-mhrd/pkg/user' 'github.com/dgrijalva/jwt-go' 'github.com/spf13/viper')
func register(svc user.Service) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { views.Wrap(views.ErrMethodNotAllowed, w) return }
var user user.User if err := json.NewDecoder(r.Body).Decode(&user); err != nil { views.Wrap(err, w) return }
u, err := svc.Register(r.Context, user.Email, user.Password, user.PhoneNumber) if err != nil { views.Wrap(err, w) return } w.WriteHeader(http.StatusCreated) token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ 'email': u.Email, 'id': u.ID, 'role': 'user', }) tokenString, err := token.SignedString(byte(viper.GetString('jwt_secret'))) if err != nil { views.Wrap(err, w) return } json.NewEncoder(w).Encode(map[string]interface{}{ 'token': tokenString, 'user': u, }) return })}
func login(svc user.Service) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { views.Wrap(views.ErrMethodNotAllowed, w) return } var user user.User if err := json.NewDecoder(r.Body).Decode(&user); err != nil { views.Wrap(err, w) return }
u, err := svc.Login(r.Context, user.Email, user.Password) if err != nil { views.Wrap(err, w) return }
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ 'email': u.Email, 'id': u.ID, 'role': 'user', }) tokenString, err := token.SignedString(byte(viper.GetString('jwt_secret'))) if err != nil { views.Wrap(err, w) return } json.NewEncoder(w).Encode(map[string]interface{}{ 'token': tokenString, 'user': u, }) return })}
func profile(svc user.Service) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// @protected // @description build profile if r.Method == http.MethodPost { var user user.User if err := json.NewDecoder(r.Body).Decode(&user); err != nil { views.Wrap(err, w) return }
claims, err := middleware.ValidateAndGetClaims(r.Context, 'user') if err != nil { views.Wrap(err, w) return } user.Email = claims['email'].(string) u, err := svc.BuildProfile(r.Context, &user) if err != nil { views.Wrap(err, w) return }
json.NewEncoder(w).Encode(u) return } else if r.Method == http.MethodGet {
// @description view profile claims, err := middleware.ValidateAndGetClaims(r.Context, 'user') if err != nil { views.Wrap(err, w) return } u, err := svc.GetUserProfile(r.Context, claims['email'].(string)) if err != nil { views.Wrap(err, w) return }
json.NewEncoder(w).Encode(map[string]interface{}{ 'message': 'User profile', 'data': u, }) return } else { views.Wrap(views.ErrMethodNotAllowed, w) return } })}
func changePassword(svc user.Service) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { var u user.User if err := json.NewDecoder(r.Body).Decode(&u); err != nil { views.Wrap(err, w) return }
claims, err := middleware.ValidateAndGetClaims(r.Context, 'user') if err != nil { views.Wrap(err, w) return } if err := svc.ChangePassword(r.Context, claims['email'].(string), u.Password); err != nil { views.Wrap(err, w) return } return } else { views.Wrap(views.ErrMethodNotAllowed, w) return } })}
// expose handlersfunc MakeUserHandler(r *http.ServeMux, svc user.Service) { r.Handle('/api/v1/user/ping', http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) return })) r.Handle('/api/v1/user/register', register(svc)) r.Handle('/api/v1/user/login', login(svc)) r.Handle('/api/v1/user/profile', middleware.Validate(profile(svc))) r.Handle('/api/v1/user/pwd', middleware.Validate(changePassword(svc)))}

錯誤處理

整潔架構(gòu)的正確之路

整潔架構(gòu)中錯誤處理的基本原則如下。

倉庫級錯誤應該是統(tǒng)一的,對于每個接口適配器來說,應該以不同的方式進行封裝和實現(xiàn)。

這本質(zhì)上的意思是,所有的數(shù)據(jù)庫級錯誤都應該由用戶接口以不同的方式來處理。例如,如果用戶接口是一個 REST API,那么錯誤應該以 HTTP 狀態(tài)碼的形式表現(xiàn)出來,比如 500 錯誤。而如果是 CLI 方式,則應該以狀態(tài)碼 1 退出。

在整潔架構(gòu)中,Repository 錯誤可以在 pkg 的根目錄下,這樣 Repository 函數(shù)就可以在控制流出現(xiàn)問題時調(diào)用它們,如下圖所示。

package errors
import ( 'errors')
var ( ErrNotFound = errors.New('Error: Document not found') ErrNoContent = errors.New('Error: Document not found') ErrInvalidSlug = errors.New('Error: Invalid slug') ErrExists = errors.New('Error: Document already exists') ErrDatabase = errors.New('Error: Database error') ErrUnauthorized = errors.New('Error: You are not allowed to perform this action') ErrForbidden = errors.New('Error: Access to this resource is forbidden'))

pkg/errors.go

然后,同樣的錯誤可以根據(jù)具體的用戶界面來實現(xiàn),最常見的是可以在 Handler 層面在 view 中進行封裝,如下圖所示。

package views
import ( 'encoding/json' 'errors' 'net/http'
log 'github.com/sirupsen/logrus'
pkg 'github.com/L04DB4L4NC3R/jobs-mhrd/pkg')
type ErrView struct { Message string `json:'message'` Status int `json:'status'`}
var ( ErrMethodNotAllowed = errors.New('Error: Method is not allowed') ErrInvalidToken = errors.New('Error: Invalid Authorization token') ErrUserExists = errors.New('User already exists'))
var ErrHTTPStatusMap = map[string]int{ pkg.ErrNotFound.Error: http.StatusNotFound, pkg.ErrInvalidSlug.Error: http.StatusBadRequest, pkg.ErrExists.Error: http.StatusConflict, pkg.ErrNoContent.Error: http.StatusNotFound, pkg.ErrDatabase.Error: http.StatusInternalServerError, pkg.ErrUnauthorized.Error: http.StatusUnauthorized, pkg.ErrForbidden.Error: http.StatusForbidden, ErrMethodNotAllowed.Error: http.StatusMethodNotAllowed, ErrInvalidToken.Error: http.StatusBadRequest, ErrUserExists.Error: http.StatusConflict,}
func Wrap(err error, w http.ResponseWriter) { msg := err.Error code := ErrHTTPStatusMap[msg]
// If error code is not found // like a default case if code == 0 { code = http.StatusInternalServerError }
w.WriteHeader(code)
errView := ErrView{ Message: msg, Status: code, } log.WithFields(log.Fields{ 'message': msg, 'code': code, }).Error('Error occurred')
json.NewEncoder(w).Encode(errView)}

每個 Repository 級別的錯誤,或者其他的錯誤,都會被封裝在一個 map 中,該 map 返回一個與相應的錯誤相對應的 HTTP 狀態(tài)代碼。

總結(jié)

整潔架構(gòu)是一個很好的構(gòu)造代碼的方法,并可以忘記所有可能由于敏捷迭代或快速原型而產(chǎn)生的復雜問題。由于和數(shù)據(jù)庫、用戶界面,以及框架無關(guān),整潔架構(gòu)確實名副其實。

(小編注:看完本文,如果你還有些疑惑,建議閱讀鏈接1項目代碼后,再來結(jié)合文章看)

參考資料

(1) Clean Architecture Sample

https://github.com/L04DB4L4NC3R/clean-architecture-sample

(2) Clean Coder Blog

https://blog./uncle-bob/2012/08/13/the-clean-architecture.html

英文原文:

https:///gdg-vit/clean-architecture-the-right-way-d83b81ecac6

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多