What is Data Transfer Object?

前端 未结 8 983
庸人自扰
庸人自扰 2020-11-22 10:35

What is a Data Transfer Object?

In MVC are the model classes DTO, and if not what are the differences and do we need both?

相关标签:
8条回答
  • 2020-11-22 10:39
    1. To me the best answer to the question what is a DTO is that DTO's are simple objects that should not contain any business logic or methods implementation that would require testing.
    2. Normally your model (using the MVC pattern) are intelligent models, and they can contain a lot of/some methods that do some different operations for that model specifically (not business logic, this should be at the controllers). However, when you transfer data (eg. calling a REST (GET/POST/whatever) endpoint from somewhere, or consuming a webservice using SOA, etc...) you do not want to transmit the big sized object with code that is not necessary for the endpoint, will consume data, and slow down the transfer.
    0 讨论(0)
  • 2020-11-22 10:42

    Data transfer object (DTO) describes “an object that carries data between processes” (Wikipedia) or an “object that is used to encapsulate data, and send it from one subsystem of an application to another” (Stack Overflow answer).

    0 讨论(0)
  • 2020-11-22 10:43

    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

    0 讨论(0)
  • 2020-11-22 10:44

    A DTO is a dumb object - it just holds properties and has getters and setters, but no other logic of any significance (other than maybe a compare() or equals() implementation).

    Typically model classes in MVC (assuming .net MVC here) are DTOs, or collections/aggregates of DTOs

    0 讨论(0)
  • 2020-11-22 10:44

    With MVC data transfer objects are often used to map domain models to simpler objects that will ultimately get displayed by the view.

    From Wikipedia:

    Data transfer object (DTO), formerly known as value objects or VO, is a design pattern used to transfer data between software application subsystems. DTOs are often used in conjunction with data access objects to retrieve data from a database.

    0 讨论(0)
  • 2020-11-22 10:50

    The definition for DTO can be found on Martin Fowler's site. DTOs are used to transfer parameters to methods and as return types. A lot of people use those in the UI, but others inflate domain objects from them.

    0 讨论(0)
提交回复
热议问题