*ps I'm just updating this to my recent project structure. It may be different from the others, but it should be easy to learn as the first start before learning more techniques and pattern
Move all interfaces from domain to the consuming side. E.g., the rest/article.go layer depends on the service so that it will consume the ArticleService interface. Instead of referring to the domain, the interface will be declared in the rest/article.go package. The same goes for repositories interfaces that are declared in article/service.go
// path: internal/rest/article.go
type ArticleService interface {
Fetch(ctx context.Context, cursor string, num int64) ([]domain.Article, string, error)
GetByID(ctx context.Context, id int64) (domain.Article, error)
Update(ctx context.Context, ar *domain.Article) error
GetByTitle(ctx context.Context, title string) (domain.Article, error)
Store(context.Context, *domain.Article) error
Delete(ctx context.Context, id int64) error
}
type ArticleHandler struct {
Service ArticleService
}
const defaultNum = 10
func NewArticleHandler(e *echo.Echo, svc ArticleService) {
handler := &ArticleHandler{
Service: svc,
}
e.GET("/articles", handler.FetchArticle)
e.POST("/articles", handler.Store)
e.GET("/articles/:id", handler.GetByID)
e.DELETE("/articles/:id", handler.Delete)
}
2. internal package
The internal package hides the service-specific detail implementation, e.g., database, rest, cache, etc. Moving this implementation to internal will hide the implementations if the project is imported from another project. But it still shows all the core logic (domain and service).
The article package will only contain all business logic related to the article domain <> in the previous version it contains all (service, repository, controller).
And this package now will called as service layer.
Code Directory V4
*ps I'm just updating this to my recent project structure. It may be different from the others, but it should be easy to learn as the first start before learning more techniques and pattern
New Code structure :
Changelogs
1. Declare Interface to the consuming side
Move all interfaces from
domain
to the consuming side. E.g., therest/article.go
layer depends on the service so that it will consume theArticleService
interface. Instead of referring to thedomain
, the interface will be declared in therest/article.go
package. The same goes for repositories interfaces that are declared inarticle/service.go
2. internal package
The
internal
package hides the service-specific detail implementation, e.g., database, rest, cache, etc. Moving this implementation tointernal
will hide the implementations if the project is imported from another project. But it still shows all the core logic (domain and service).3. Service-focused package
The
article
package will only contain all business logic related to the article domain <> in the previous version it contains all (service, repository, controller). And this package now will called as service layer.Previously
New Structure