Objects that are used to associate with presentation levels are usually called view models, and they are DTOs designed to display data displayed from domain objects and then map user input back to domain objects. View models usually look very similar to the domain objects that they represent, but there are some important differences:
The data of domain objects can be flattened or otherwise transformed in accordance with the requirements of this type. Having mappings in simple objects is easier to manage than mappings in a view structure such as MVC. Easier to debug and detect errors.
For this view, data from several domain objects may be required - there cannot be an object with one domain that meets the requirements of the view. A view model can be populated with several domain objects.
A presentation model is usually developed taking into account the specific structure of the presentation and, as such, can use infrastructure-specific attributes for binding and validation on the client side. As you said, a typical requirement is a parameterless constructor, which works great for a view model. Again, itβs much easier to test and manage the view model than some kind of complex rendering engine.
The presented models, apparently, violate the DRY principle, however, after closer attention, the responsibility of the viewing model differs, therefore, with the principle of single responsibility, this article discusses the error of reuse, often cited according to the DRY principle.
In addition, view models are truly anemic, although they can have a constructor that takes a domain object as a parameter and a way to create and update a domain object using the values ββin the view model as input. From experience, I believe that it is good practice to create a presentation model class for each domain object that will be displayed by the presentation level. Itβs easier to manage a hierarchy of double classes of domain objects and a view model than to manage complex display mechanisms.
In addition, there are libraries that try to simplify the mapping between view models and domain objects, such as AutoMapper for the .NET Framework.
eulerfx
source share