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 convince my colleagues that this is a good practice, but I don't seem to be making much headway...
They use domain entities where ever they please in the presentation and interface layers. When I argue to them that they should be using display models or DTOs to insulate the Domain layer from the interface layer, they counter that they don't see the business value in doing something like that, because now you have a UI object to maintain as well as the original domain object.
So I'm looking for some concrete reasons I can use to back this up. Specifically:
- Why should we not use domain objects in our presentation layer?
(if the answer is the obvious one, 'decoupling', then please explain why this is important in this context) - Should we use additional objects or constructs to isolate our domain objects from the interface?
Dammit, I swear this said persistence.
Anyway, it's one more instance of the same thing: Parnas's law says a module should keep a secret, and the secret is a requirement that can change. (Bob Martin has a rule that's another version of this.) In a system like this, the presentation can change independently of the domain. Such as, for example, a company that maintains prices in Euros and uses French in the company offices, but wants to present prices in dollars with text in Mandarin. The domain is the same; the presentation can change. So, to minimize the brittleness of the system — that is, the number of things that must be changed to implement a change in requirements — you separate the concerns.
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 two reasons:
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.