Skip to content

关于前后端“同名不同模”的结构优化 #187

@Goodjooy

Description

@Goodjooy

在当前项目中,存在大量的由于前后台的结构不同带来的相同的数据却有着不同的具体结构的情况存在,在很多时候,都需要同时使用2种类似的struct来区分前后端。

在当前情况下,“同名不同模” 有以下的一些问题

  • 命名困难:为了区分模型使用位置,使得模型需要在类型名称中进行区分
  • 冗余代码:相近的类型带来了相近的代码,这使得代码出现大量重复。

改进想法:

  • 使用基于定制trait + 辅助宏的方式对这些代码改进

trait方面

  • ViewModelTrait trait提供关联类型FrontendBackend, 用于分别定位到具体的前端\后端响应模型
  • IntoFrontendTryIntoFrontend 用于从ViewModelTrait 转换到前台类型
  • IntoBackendTryIntoBackend 用于从ViewModelTrait 转换到后台类型

宏方面
提供一个派生宏,提供ViewModelTrait 的自动派生

  • field参数:
    • backend(ignore) / frontend(ignore)/ignore 后端/前端/双端忽视这个字段
    • backend(conv(ty=, err=, conv=)) / frontend(conv(ty=, err=, conv=)) /conv(ty=, err=, conv=) 后台/前台/两者都 提供类型转换,如果给定err将会实现TryIntoXXX, 否则实现IntoXXX

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions