
我目前正在学习 Go,我很感激人们对如何最好地减少重复代码量的见解。
相关部分的文件夹结构是这样的:
.
├── http
│ ├── handlers
│ └── routes
├── models
│ └── dto
├── specifications
└── store
└── postgres在我的 specations 文件夹中,我有 2 个“存储”接口:
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 文件夹中,我有一个结构体,它接受存储接口之一的输入:
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 方法基本相同,但每个商店使用的接口除外。
如何更改此设置以减少重复代码?
我最初认为我可以使用泛型来解决这个问题,例如:
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 和接口有什么建议吗?
你可以有一个辅助函数
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 中映射 DTO 时减少重复代码的数量的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号