• Data Transfer Object


    What is Data Transfer Object?

    A Data Transfer Object is an object that is used to encapsulate data, and send it from one subsystem of an application to another.

    DTOs are most commonly used by the Services layer in an N-Tier application to transfer data between itself and the UI layer. The main benefit here is that it reduces the amount of data that needs to be sent across the wire in distributed applications. They also make great models in the MVC pattern.

    Another use for DTOs can be to encapsulate parameters for method calls. This can be useful if a method takes more than 4 or 5 parameters.

    When using the DTO pattern, you would also make use of DTO assemblers. The assemblers are used to create DTOs from Domain Objects, and vice versa.

    The conversion from Domain Object to DTO and back again can be a costly process. If you're not creating a distributed application, you probably won't see any great benefits from the pattern, as Martin Fowler explains here

    Data Transfer Object

    An object that carries data between processes in order to reduce the number of method calls.

    For a full description see P of EAA page 401

    When you're working with a remote interface, such as Remote Facade (388), each call to it is expensive. As a result you need to 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.

    Many people in the Sun community use the term "Value Object" for this pattern. I use it to mean something else. See the discussion on page 487.

    Although the main reason for using a Data Transfer Object is to batch up what would be multiple remote calls into a single call, it's worth mentioning that another advantage is to encapsulate the serialization mechanism for transferring data over the wire. 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.

    The Sun/Java community's use of terminology has changed since the book was published. They switched to using "Transfer Object" as the name for this pattern. Consequently "Value Object" became regularly used in the way that I describe in this book.

    What is the point of using DTO (Data Transfer Objects)?

    DTO is a pattern and it is implementation (POJO/POCO) independent. DTO says, since each call to any remote interface is expensive, response to each call should bring as much data as possible. So, if multiple requests are required to bring data for a particular task, data to be brought can be combined in a DTO so that only one request can bring all the required data. Catalog of Patterns of Enterprise Application Architecture has more details.

    DTO's are a fundamental concept, not outdated.

    Difference between Entity and DTO

    Difference between DTO & Entity:

    Entity is class mapped to table. Dto is class mapped to "view" layer mostly.

    What needed to store is entity & which needed to 'show' on web page is DTO.

    LocalDTO

    If you've been keeping an eye on my fellow ThoughtBloggers you'll know that it seems one of my fowlbots has blown a fuse, the Australian sun obviously frizzles these Swedish models.

    Jon's annoyed with Data Transfer Objects, but it's not that DTOs are a bad thing, just like any pattern they are useful in a certain context. Patterns always have two parts: the how and the when. Not just do you need to know how to implement them, you also have to know when to use them and when to leave them alone.

    He does have a point - although I only talk about them in the context of remote interfaces, I didn't reiterate their remote context in the write up of the pattern itself - and I do run into people who like to use them in non-remote cases. So here's my chance to make up.

    DTOs are called Data Transfer Objects because their whole purpose is to shift data in expensive remote calls. They are part of implementing a coarse粗糙的 grained interface which a remote interface needs for performance. Not just do you not need them in a local context, they are actually harmful both because a coarse-grained API is more difficult to use and because you have to do all the work moving data from your domain or data source layer into the DTOs.

    Some people argue for them as part of a Service Layer API because they ensure that service layer clients aren't dependent upon an underlying Domain Model. While that may be handy便利的, I don't think it's worth the cost of all of that data mapping. As my contributor Randy Stafford says in P of EAA "Don't underestimate the cost of [using DTOs].... It's significant, and it's painful - perhaps second only to the cost and pain of object-relational mapping".

    Another argument I've heard is using them in case you want to distribute later. This kind of speculative distribution boundary is what I rail against with the FirstLaw. Adding remote boundaries adds complexity. So I'd echo Randy's advice "start with a locally invocable Service Layer whose method signatures deal in domain objects. Add remotability when you need it (if ever) by putting Remote Facades on your Service Layer or having your Service Layer objects implement remote interfaces."

    One case where it is useful to use something like a DTO is when you have a significant mismatch between the model in your presentation layer and the underlying domain model. In this case it makes sense to make presentation specific facade/gateway that maps from the domain model and presents an interface that's convenient for the presentation. It fits in nicely with Presentation Model. I hope to talk about this more in the new volume. This is worth doing, but it's only worth doing for screens that have this mismatch (in this case it isn't extra work, since you'd have to do it in the screen anyway.)

    Update: Thibaut Barrère reminded me of another purpose for Local DTOs, communicating between isolates in multi-threaded applications. A good way to handle multi-threading is to partition your application into isolated areas where each area has only one thread running over it. This eliminates the needs for concurrency controls within that area. If you need to communicate between these isolated areas, then use a message queue with DTOs as messages to transfer the information. 

  • 相关阅读:
    apache一个IP多个站点的配置方法
    论文笔记之:Deep Reinforcement Learning with Double Q-learning
    论文笔记之:Playing Atari with Deep Reinforcement Learning
    论文笔记之:Learning Multi-Domain Convolutional Neural Networks for Visual Tracking
    论文笔记之:Learning to Track: Online Multi-Object Tracking by Decision Making
    论文笔记之:Human-level control through deep reinforcement learning
    (译) 强化学习 第一部分:Q-Learning 以及相关探索
    论文笔记之:Multiple Object Recognition With Visual Attention
    论文笔记之:Attention For Fine-Grained Categorization
    新手教程之:循环网络和LSTM指南 (A Beginner’s Guide to Recurrent Networks and LSTMs)
  • 原文地址:https://www.cnblogs.com/chucklu/p/10784405.html
Copyright © 2020-2023  润新知