数据传输对象(DTO)是否也适用于绑定UI?

发布于 2025-01-26 09:47:20 字数 384 浏览 5 评论 0原文

我的问题很简单:

我有一个大数据传输对象,例如:

data class Terminal(
  @Expose
  @SerializedName("inspector_code")
  var inspectorCode:String,

  @Expose
  @SerializedName("inspector_id")
  var inspectorId:Long
  [x50]
)

大多数用于业务逻辑,用于HTTP请求,交易验证,

其中很少用于UI。

这种类型的DTO也应该在演示层中使用吗?或者我可以使用一个不同的一个并使用转换器来构建演示对象,它甚至在MVVM arhitecture中是否存在?

My question is simple:

I have a large Data Transfer Object like:

data class Terminal(
  @Expose
  @SerializedName("inspector_code")
  var inspectorCode:String,

  @Expose
  @SerializedName("inspector_id")
  var inspectorId:Long
  [x50]
)

Most of them are used for the business logic, for http requests, transaction verifying,

few of them are used for the UI.

Should this type of DTO be used in the presentation layer too ? or i can have a different one and use converters to build a presentation object, does this even exists in mvvm arhitecture?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伏妖词 2025-02-02 09:47:20

DTO应转换为本地模型类。这确实消除了端点和UI的耦合,并允许更灵活的体系结构。

翻译DTO可以在像存储库之类的东西中完成,该存储库又使用您的数据源返回DTO。

由于这个问题似乎是针对Android(Android-MVVM)的,因此您可以在此处查看本指南, https://developer.android.com/topic/architecture

The DTO should be converted to a local model class. This does remove the coupling of your endpoints and your UI and allows a more flexible architecture.

Translating a DTO can be done in something like a Repository, which in turn uses your datasource that returns the DTO.

Since the question looks to be targeted at Android (Android-mvvm), you can have a look at this guide here as well https://developer.android.com/topic/architecture

泪之魂 2025-02-02 09:47:20

我个人在每个DTO类中添加了一种方法,使我可以将其转换为模型对象。如果模型或DTO类有任何更改,这将导致编译时间的例外,以通知我需要调整此功能。有时,您还需要将对象从模型转换为DTO类型,在这里,我通常也更喜欢将此功能或构造函数放在DTO类中。这样一来,我的体系结构的较高层(即域层)就我正在使用的后端的特点而言仍然不可知。

在这里,我预示着我的后端无法直接存储布尔值。如图所示,我的bean模型对象对如何存储它是不知道和

data class Bean(val name:String, val isApproved:Boolean)


data class BeanDto(val name:String, val isApproved:Int){

   constructor(bean:Bean) : this(name=bean.name, isApproved=if (bean.isApproved) 1 else 0) 

   fun toBean()=Bean(
       name=name,
       isApproved = isApproved==1
   )
}

无动于衷

I personally add a method in every Dto class that allows me to turn it into a Model object. If there are any changes in either the the model or the Dto class this will result in an exception at compile time notifying me of the need to adjust this function. sometimes you also need to turn objects from Model into Dto types and here I generally also prefer to put this function or constructor in the Dto class; that way the higher layers of my architecture (namely the domain layer) remain agnostic as to the peculiarities of the backend that I am using.

here an example where I presune that my backend is incapable of storing booleans directly. As is shown my Bean model object is unaware and indifferent as to how it is stored all the loginc is directly attatched to the Dto

data class Bean(val name:String, val isApproved:Boolean)


data class BeanDto(val name:String, val isApproved:Int){

   constructor(bean:Bean) : this(name=bean.name, isApproved=if (bean.isApproved) 1 else 0) 

   fun toBean()=Bean(
       name=name,
       isApproved = isApproved==1
   )
}

I am happy to hear comments regarding what others think about this approach

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文