我目前正在学习 Go,我很感激人们对如何最好地减少重复代码量的见解。
相关部分的文件夹结构是这样的:
.
├── http
│ ├── handlers
│ └── routes
├── models
│ └── dto
├── specifications
└── store
└── postgres
在我的规范
文件夹中,我有2个“商店”接口(interface):
type TaskStore interface {
CreateTask(ctx context.Context, input dto.TaskCreate) error
UpdateTask(ctx context.Context, id int, input dto.TaskUpdate) error
GetTask(ctx context.Context, id int) (dto.TaskResult, error)
ListTasks(ctx context.Context) ([]dto.TaskResult, error)
DeleteTask(ctx context.Context, id int) error
}
type TagStore interface {
CreateTag(ctx context.Context, input dto.TagCreate) error
RenameTag(ctx context.Context, id int, input dto.TagUpdate) error
ListTags(ctx context.Context) ([]dto.TagResult, error)
GetTag(ctx context.Context, id int) (dto.TagResult, error)
DeleteTag(ctx context.Context, id int) error
}
store/postgres
文件夹包含任务和标签(存储库模式)的实现。
我看到的问题:
在我的 handlers
文件夹中,我有一个结构体,它接受存储接口(interface)之一的输入:
type TaskHandler struct {
store specifications.TaskStore
}
func NewTaskHandler(store specifications.TaskStore) TaskHandler {
return TaskHandler{
store: store,
}
}
type TagHandler struct {
store specifications.TagStore
}
func NewTagHandler(store specifications.TagStore) TagHandler {
return TagHandler{
store: store,
}
}
这些处理程序包含将映射到 api 路径的方法:
func (h TaskHandler) List() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tasks, err := h.store.ListTasks(r.Context())
if err != nil {
log.Err(err).Msg("failed to retrieve tasks")
w.WriteHeader(http.StatusInternalServerError)
return
}
render.JSON(w, r, tasks)
}
}
func (h TagHandler) List() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tags, err := h.store.ListTags(r.Context())
if err != nil {
log.Err(err).Msg("failed to retrieve tags")
w.WriteHeader(http.StatusInternalServerError)
return
}
render.JSON(w, r, tags)
}
}
您会注意到,每个处理程序上的 List
方法基本相同,但每个商店使用的接口(interface)除外。
如何更改此设置以减少重复代码?
我最初认为我可以使用泛型来解决这个问题,例如:
type EntityStore[CreateDto any, UpdateDto any, ResultDto any] interface {
Create(ctx context.Context, input CreateDto) error
Update(ctx context.Context, id int, input UpdateDto) error
List(ctx context.Context) ([]ResultDto, error)
Get(ctx context.Context, id int) (ResultDto, error)
Delete(ctx context.Context, id int) error
}
但这意味着将每种类型映射到处理程序中,我认为这不是一个实用的解决方案。
关于如何更好地映射我的 DTO 和接口(interface)有什么建议吗?
最佳答案
你可以有一个辅助函数
func ListHandler[T any](name string, lister func(ctx context.Context) ([]T, error)) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
list, err := lister(r.Context())
if err != nil {
log.Err(err).Msg("failed to retrieve " + name)
w.WriteHeader(http.StatusInternalServerError)
return
}
render.JSON(w, r, list)
}
}
然后你就会有
func (h TaskHandler) List() http.HandlerFunc {
return ListHandler("tasks", h.store.ListTasks)
}
func (h TagHandler) List() http.HandlerFunc {
return ListHandler("tags", h.store.ListTags)
}
关于go - 在 Go 中映射 DTO 时减少重复代码的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77686417/