Entities VS Domain Models VS View Models

前端 未结 3 1774
盖世英雄少女心
盖世英雄少女心 2020-12-04 06:05

There are hundreds of similar questions on this topic. But I am still confused and I would like to get experts advise on this.

We are developing an application using

3条回答
  •  猫巷女王i
    2020-12-04 06:08

    The main benefit of introducing additional classes is separation of concerns:

    • Presentation layer: display information and data entry, including any pre- and postprocessing to achieve this, e.g. formatting.
    • Domain / Business / Application logic: here the actual work is done
    • Persistence layer: store and retrieve data

    This can be achieved with only two model classes for ViewModels and Domain Entities. Instead of modelling the domain logic with separate model classes that are similar to the persisted entities, implement it in service classes that consume the domain entities. The domain entities should at most have logic that deals with their own properties (e.g. to keep the combined values of two properties in a valid state). If more than one domain entity is affected by an usecase, model this usecase in a service class. If you put logic to manage the relationship between entities directly in the entity classes, the code becomes unmaintainable very quickly (trust me, I tried).

    I do not recommend using the persistable entities as ViewModels, because this would mix display concerns (e.g. [DisplayName]) with persistence concerns (e.g. [ForeignKey]).

提交回复
热议问题