是一个视图模型和数据传输对象之间的区别? [英] what is the difference between a view model and a data transfer object?

查看:82
本文介绍了是一个视图模型和数据传输对象之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我立足于福勒POEAA这个问题。鉴于你熟悉这段文字,是不是在ASP.NET MVC一样的DTO使用的ViewModels?为什么或者为什么不?谢谢。

I'm basing this question on Fowler PoEAA. Given your familiarity with this text, aren't the ViewModels used in ASP.NET MVC the same as DTOs? Why or why not? Thank you.

推荐答案

他们为类似的目的(用于应用程序的另一个层封装数据),但他们做的不同,不同的原因。

They serve a similar purpose (encapsulating data for another layer of the application) but they do it differently and for different reasons.


  • 在进行DTO的目的是减少一个应用层之间的调用次数,尤其是当这些呼叫是昂贵的(例如,分布式系统)。 DTO的几乎都是平凡序列化,而且几乎从来没有包含任何行为。

  • The purpose of a DTO is to reduce the number of calls between tiers of an application, especially when those calls are expensive (e.g. distributed systems). DTOs are almost always trivially serializable, and almost never contain any behaviour.

例如,你开发一个电子商务网站。 CreateCustomer AddCustomerAddress 是在数据库级独立操作,但可能由于性能原因希望将自己的数据聚合成一个 NewCustomerWithAddressDto ,使您的客户端只需要做出一个往返到服务器,并不需要关心服务器可能会做一堆用不同的东西数据包。

For example, you're developing an e-Commerce site. CreateCustomer and AddCustomerAddress are separate operations at the database level, but you might for performance reasons want to aggregate their data into a NewCustomerWithAddressDto so that your client only needs to make one round-trip to the server, and doesn't need to care that the server might be doing a bunch of different things with the parcel of data.

术语视图模型是指MV *的不同口味略有不同的东西,但其目的主要是关注点分离。模型经常超过presentation一些其他的目的进行了优化,它是视图模型的责任解耦查看从模型的实现细节。此外,大多数MV *模式建议让您的意见是哑巴成为可能,因此视图模型有时花费presentation逻辑责任。

The term "ViewModel" means slightly different things in different flavours of MV*, but its purpose is mainly separation of concerns. Your Model is frequently optimised for some purpose other than presentation, and it's the responsibility of the ViewModel to decouple your View from the Model's implementation details. Additionally, most MV* patterns advise making your Views as "dumb" as possible, and so the ViewModel sometimes takes responsibility for presentation logic.

例如,在同一个电子商务应用程序,你的 CustomerModel 是错误的形为你新客户查看presentation。对于初学者来说,你的观点对您的用户密码和确认密码,以及两个表单字段的 CustomerModel 不包含一个密码字段在所有!你的 NewCustomerViewModel 将包含这些领域,可能,这取决于你的MV *味道,负责一些presentation逻辑(例如,显示视图/隐藏部分)和基本验证(如确保这两个密码字段匹配)。

For example, in the same e-Commerce application, your CustomerModel is the wrong "shape" for presentation on your "New Customer" View. For starters, your View has two form fields for your user to enter and confirm their password, and your CustomerModel doesn't contain a password field at all! Your NewCustomerViewModel will contain those fields and might, depending on your flavour of MV*, be responsible for some presentation logic (e.g. to show/hide parts of the view) and basic validation (e.g. ensuring that both password fields match).

这篇关于是一个视图模型和数据传输对象之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆