DTO vs POCO vs Value Object

    In this article, I would like to clarify the differences between DTO (Data Transfer Object), Value Object and POCO (Plain Old CLR Object), also known as POJO in the Java environment.

    DTO, POCO, and Value Object Definitions


    First, a small remark about the Value Object. In C #, there is a similar concept called Value Type. This is just a detail of the implementation of how objects are stored in memory and we will not touch on this. The Value Object to be discussed is a concept from the DDD (Domain-Driven Design) environment.

    Ok, let's get started. You may have noticed that concepts such as DTO, Value Object, and POCO are often used synonymously. But do they really mean the same thing?

    DTO is a class containing data without any logic for working with them. DTOs are typically used to transfer data between different applications, or between layers within a single application. They can be considered as a repository of information, the sole purpose of which is to transfer this information to the recipient.

    On the other hand, Value Object is a full member of your domain model. It follows the same rules as Entities. The only difference between the Value Object and Entity is that the Value Object does not have its own identity. This means that two Value Objects with the same properties can be considered identical, while two entities are different from each other even if their properties coincide completely.

    Value Objects can contain logic and are usually not used to transfer information between applications.

    POCO (Plain Old CLR Object) is a term created as an analogy for POJO. POJO cannot be used in .NET because the letter “J” in it means “Java”. POCO has the same semantics as POJO.

    POJO was introduced by Martin Fowler as an alternative to JavaBeans and other "heavy" enterprise constructs that were popular in the early 2000s.

    The main goal of POJO was to show that the application domain can be successfully modeled without using JavaBeans. Moreover, JavaBeans should not be used for this purpose at all.

    In the .NET environment there is no direct analogy for JavaBeans, as Microsoft has never imagined anything similar, but we can come up with some parallel.

    You can think of the component classfrom System.ComponentModel as the opposite of POCO. There are many classes in .NET that inherit from Component, such as DBCommand from System.Data or EventLog from System.Diagnostics.

    In most cases, it makes no sense to inherit domain classes from Component. this solution introduces unnecessary complexity into the model. Using heavyweight classes from the .NET Framework for such purposes is contrary to the YAGNI principle .

    Another good example of an anti-POCO approach is the Entity Framework prior to version 4.0. Each class generated by EF inherited from EntityObject, which introduced EF-specific logic into the domain. Starting with version 4, the Entity Framework added the ability to work with the POCO model - the ability to use classes that are not inherited from EntityObject.

    Thus, the concept of POCO means the use of as simple classes as possible for modeling the subject area . This concept helps to adhere to the principles of YAGNI, KISS and other best practices. POCO classes may contain logic .

    Correlation between concepts


    Are there any connections between these three concepts? First of all, DTO and Value Object reflect different concepts and cannot be used interchangeably. POCO, on the other hand, is a superset of DTO and Value Object:

    image

    In other words, Value Object and DTO do not inherit from any third-party components and thus are POCO. At the same time, POCO is a broader concept: it can be a Value Object, Entity, DTO, or any other class if it does not inherit components that are not directly related to the problem you are solving.

    Here are the properties of each of them:

    image

    Note that a POCO class may or may not have its own identity, as it can be either a Value Object or an Entity. Also, POCO may or may not contain logic within itself. It depends on whether the POCO DTO.

    Conclusion


    The above can be summarized as follows:

    • DTO! = Value Object
    • DTO ⊂ POCO
    • Value Object ⊂ POCO

    English version of the article: DTO vs Value Object vs POCO

    Also popular now: