What is Data Transfer Object?

前端 未结 8 985
庸人自扰
庸人自扰 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:55

    DefN

    A DTO is a hardcoded data model. It only solves the problem of modeling a data record handled by a hardcoded production process, where all fields are known at compile-time and therefore accessed via strongly typed properties.

    In contrast, a dynamic model or "property bag" solves the problem of modeling a data record when the production process is created at runtime.

    The Cvar

    A DTO can be modeled with fields or properties, but someone invented a very useful data container called the Cvar. It is a reference to a value. When a DTO is modeled with what I call reference properties, modules can be configured to share heap memory and thereby collaboratively work on it. This completely eliminates parameter passing and O2O communication from your code. In other words, DTOs having reference properties allow code to achieve zero coupling.

        class Cvar { ... }
    
        class Cvar<T> : Cvar
        {
            public T Value { get; set; }
        }
    
        class MyDTO
        {
            public Cvar<int> X { get; set; }
            public Cvar<int> Y { get; set; }
            public Cvar<string> mutableString { get; set; } // >;)
        }
    

    Source: http://www.powersemantics.com/

    Dynamic DTOs are a necessary component for dynamic software. To instantiate a dynamic process, one compiler step is to bind each machine in the script to the reference properties the script defines. A dynamic DTO is built by adding the Cvars to a collection.

        // a dynamic DTO
        class CvarRegistry : Dictionary<string, Cvar> { }
    

    Contentions

    Note: because Wix labeled the use of DTOs for organizing parameters as an "anti-pattern", I will give an authoritative opinion.

        return View(model);  // MVC disagrees
    

    My collaborative architecture replaces design patterns. Refer to my web articles.

    Parameters provide immediate control of a stack frame machine. If you use continuous control and therefore do not need immediate control, your modules do not need parameters. My architecture has none. In-process configuration of machines (methods) adds complexity but also value (performance) when the parameters are value types. However, reference type parameters make the consumer cause cache misses to get the values off the heap anyway -- therefore, just configure the consumer with reference properties. Fact from mechanical engineering: reliance on parameters is a kind of preoptimization, because processing (making components) itself is waste. Refer to my W article for more information. http://www.powersemantics.com/w.html.

    Fowler and company might realize the benefits of DTOs outside of distributed architecture if they had ever known any other architecture. Programmers only know distributed systems. Integrated collaborative systems (aka production aka manufacturing) are something I had to claim as my own architecture, because I am the first to write code this way.

    Some consider the DTO an anemic domain model, meaning it lacks functionality, but this assumes an object must own the data it interacts with. This conceptual model then forces you to deliver the data between objects, which is the model for distributed processing. However on a manufacturing line, each step can access the end product and change it without owning or controlling it. That's the difference between distributed and integrated processing. Manufacturing separates the product from operations and logistics.

    There's nothing inherently wrong with modeling processing as a bunch of useless office workers who e-mail work to one another without keeping an e-mail trail, except for all the extra work and headache it creates in handling logistics and return problems. A properly modeled distributed process attaches a document (active routing) to the product describing what operations it came from and will go to. The active routing is a copy of the process source routing, which is written before the process begins. In the event of a defect or other emergency change, the active routing is modified to include the operation steps it will be sent to. This then accounts for all the labor which went into production.

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

    In general Value Objects should be Immutable. Like Integer or String objects in Java. We can use them for transferring data between software layers. If the software layers or services running in different remote nodes like in a microservices environment or in a legacy Java Enterprise App. We must make almost exact copies of two classes. This is the where we met DTOs.

    |-----------|                                                   |--------------|
    | SERVICE 1 |--> Credentials DTO >--------> Credentials DTO >-- | AUTH SERVICE |
    |-----------|                                                   |--------------|
    

    In legacy Java Enterprise Systems DTOs can have various EJB stuff in it.

    I do not know this is a best practice or not but I personally use Value Objects in my Spring MVC/Boot Projects like this:

            |------------|         |------------------|                             |------------|
    -> Form |            | -> Form |                  | -> Entity                   |            |
            | Controller |         | Service / Facade |                             | Repository |
    <- View |            | <- View |                  | <- Entity / Projection View |            |
            |------------|         |------------------|                             |------------|
    

    Controller layer doesn't know what are the entities are. It communicates with Form and View Value Objects. Form Objects has JSR 303 Validation annotations (for instance @NotNull) and View Value Objects have Jackson Annotations for custom serialization. (for instance @JsonIgnore)

    Service layer communicates with repository layer via using Entity Objects. Entity objects have JPA/Hibernate/Spring Data annotations on it. Every layer communicates with only the lower layer. The inter-layer communication is prohibited because of circular/cyclic dependency.

    User Service ----> XX CANNOT CALL XX ----> Order Service
    

    Some ORM Frameworks have the ability of projection via using additional interfaces or classes. So repositories can return View objects directly. There for you do not need an additional transformation.

    For instance this is our User entity:

    @Entity
    public final class User {
        private String id;
        private String firstname;
        private String lastname;
        private String phone;
        private String fax;
        private String address;
        // Accessors ...
    }
    

    But you should return a Paginated list of users that just include id, firstname, lastname. Then you can create a View Value Object for ORM projection.

    public final class UserListItemView {
        private String id;
        private String firstname;
        private String lastname;
        // Accessors ...
    }
    

    You can easily get the paginated result from repository layer. Thanks to spring you can also use just interfaces for projections.

    List<UserListItemView> find(Pageable pageable);
    

    Don't worry for other conversion operations BeanUtils.copy method works just fine.

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