Why should I isolate my domain entities from my presentation layer?

后端 未结 14 2750
南旧
南旧 2020-12-02 04:07

One part of domain-driven design that there doesn\'t seem to be a lot of detail on, is how and why you should isolate your domain model from your interface. I\'m trying to c

14条回答
  •  既然无缘
    2020-12-02 04:52

    Answer depends on scale of your application.


    Simple CRUD (Create, Read, Update, Delete) application

    For basic crud applications you do not have any functionality. Adding DTO on top of entities woudl be a waste of time. It would increase complexity without increasing scalability.


    Moderately complicated Non-CRUD application

    In this size of application you will have few entities which have true lifeclycle and some business logic associated with them.

    Adding DTOs on this case is a good idea for few reasons:

    • Presentation layer can see only subset of fields which entity has. You encapsulate Entities
    • No coupling between backend and frontent
    • If you have business methods inside entities, but not in DTO then adding DTOs means that outside code can't ruin state of your entity.


    Complicated Enterprise Application

    Single entity might need multiple ways of presentation. Each of them will need different set of fields. In this case you encounter the same issues as in previous example plus need to control amount of fields visible for each client. Having separate DTO for each client will help you to chose what should be visible.

提交回复
热议问题