了解dto概念,什么是DTO

时间:2021-02-11 17:33:25
了解dto概念     此博文收集整理了一些主流的文章对于DTO模式的解读,他们大体相似而又各有所不同。对于设计模式的解读也是一个仁者见仁智者见智的事情,不过设计模式往往都是前辈们在遇到一类特定的问题下而总结的经验和智慧。看不同大牛对同一概念的解读,对比思考,本身就是对于我们思维的一次洗礼。(所有文章均贴有出处,在此感谢大牛们的辛勤劳作。)  

什么是DTO?

百度百科如何解读的?   DTO是Data Transfer Object 的简写,既数据传输对象。 是一种设计模式之间传输数据的软件应用系统。数据传输目标往往是数据访问对象从数据库中检索的数据。数据传输对象与数据交互对象或数据访问对象之间是一个不具备有任何行为除了存储和检索的数据。(访问和存取器)   *是如何解读的?   A data transfer object(DTO) is an  object that carries data between two process. The difference between data transfer and business objects or data access objects is that a DTO does not have any behavior except for storage and retrieval of its own data(mutators and accesssors).DTOs are simple objects that should not contain any business logic that would require testing.   和百度百科不同的是,DTO和MO(Model Object)与BO(Business Object)的不同之处在于DTO没有任何业务行为(贫血模式)只作为数据的存储。   原链接:https://en.wikipedia.org/wiki/Data_transfer_object References 下有许多有料的参考文章。   有经典的文章是如何解读的?   博客园dax.net的观点:        表现层于应用层之间是通过DTO来进行交互的,数据传输对象是没有行为的POCO对象,他的目的是为了对领域对象进行数据封装,实现层与层之间的数据传递。为何不直接将领域对象进行数据传递?因为领域对象更注重领域,DTO更注重数据。由于“富领域模型”的特点,这样会直接将领域对象的行为暴露给表现层。     DTO本身不是业务对象,他是根据UI需求进行设计的。简单来说Model面向业务,我们是通过业务来定义Model的。而DTO是面向UI,通过UI的需求来定义的,通过DTO我们实现了表现层与Model层之间的解耦,表现层不引用Model。如果开发过程中我们的模型变了,而界面没变,我们只需改Model而不需要去改动表现层。   原文链接:http://www.cnblogs.com/daxnet/archive/2010/07/07/1772584.html   博客园loveis715的观点:     DTO用于在服务器与客户端之间或服务器与服务器之间进行数据传递,文章从问题出发,然后深入浅出的讨论了DTO以及相关的概念,值得一读。 原文链接:http://www.cnblogs.com/loveis715/p/4379656.html DTO – 服务实现中的核心数据   微软MSDN的观点:   Create a data transfer object(DTO) that holds all data that is required for the remote call.Modify the remote signature to accept the DTO as the single parameter and to return a single DTO to the client.After the calling application receives the DTO and and stores it as a local object , the application can make a series of individual procedure calls to the DTO without incurring the overhead of remote calls.   大致的观点是DTO可以有效的减少请求数量,文章中包含示例,讨论了问题的由来(为什么需要DTO),以及DTO使用过程中的一些注意事项。不过在文章的开头说到这个设计模式已经过时了,从此来看技术的更新迭代真的是很快,只是不知道DTO为什么过时了?以及哪些新的技术替代了DTO?   原文链接: https://msdn.microsoft.com/en-us/library/ms978717.aspx   Pattern of Enterprise  Application Architecture 作者观点:     An object carries data between two processes in order to reduce the number of method calls.          When you are working with a remote interface,such as Remote Facade,each call to it is expensive.As a result you should reduce the number of calls, and that means that you need to transfer more data with each call.One way to do this is to use  lots of parameters. However, this is often awkward  to program - indeed,it's often impossible with languages such as Java that return only a single value.       The solution is to create a Data Transfer Object that can hold all the data for the call.It needs to be serializable to go across the connection.Usually an assembler is used on the server side to transfer data between the DTO and any domain objects.           Although the main reason for using a Data Transfer Object is to batch up what would be mutiple remote calls into single call,it's worth mentioning that another advantage is to encapsulate the serialization mechanism for transferring data over write.By encapsulating the serialization like this ,the DTOs keep this logic out of the rest of the code and also provide a clear point to change  serialization should you wish.   原文链接: https://martinfowler.com/eaaCatalog/dataTransferObject.html     观点和loveis715有一致的地方,简而言之DTO的存在就是为了帮助我们减少客户端请求而降低服务器压力,提升效率。作者还有一个观点大概是指在使用DTO后我们可以灵活定义数据模型,同时将数据模型和逻辑剥离开了。