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

Technology CommunityCategory: Software ArchitectureWhy should I isolate my domain entities from my presentation layer?
VietMX Staff asked 3 years ago
Problem

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…

The problem is, as time goes on, things get added on both sides. Presentation changes, and the needs of the presentation layer evolve to include things that are completely independent of your business layer (color, for example). Meanwhile, your domain objects change over time, and if you don’t have appropriate decoupling from your interface, you run the risk of screwing up your interface layer by making seemingly benign changes to your business objects.

There are cases where a DTO makes sense to use in presentaton. Let’s say I want to show a drop down of Companies in my system and I need their id to bind the value to.

Well instead of loading a CompanyObject which might have references to subscriptions or who knows what else, I could send back a DTO with the name and id.

If you keep only one domain object, for use in the presentation AND domain layer, then that one object soon gets monolithic. It starts to include UI validation code, UI navigation code, and UI generation code. Then, you soon add all of the business layer methods on top of that. Now your business layer and UI are all mixed up, and all of them are messing around at the domain entity layer.