Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Domain-Driven Design, can you use your domain entities in your UI?

In many leading DDD projects, especially MVC style, I see the UI using display objects that mirror domain entities, rather than using those domain objects directly. This style is obviously for decoupling and separation of concerns, and I personally prefer this style.

But what I'm not sure of, is whether this a strict tenet of DDD, or whether this is more just different developers' interpretation of it.

Can you use your domain objects directly in the UI, and still be following the DDD methodology in that act?

Or is it a DDD best practice to always use display objects?

Note: While I mention MVC, I'm really interested in whether display objects must be used in almost all DDD compatible UI patterns in a DDD project.

like image 797
Mark Rogers Avatar asked Apr 22 '09 16:04

Mark Rogers


People also ask

What are entities in Domain-Driven Design?

In domain-driven design, an entity is a representation of an object in the domain. It is defined by its identity, rather than its attributes. It encapsulates the state of that object through its attributes, including the aggregation of other entities, and it defines any operations that might be performed on the entity.

Does DDD belong on the frontend?

what do you think? Does DDD belong in the front-end development space? Generally speaking, no (and I'll explain why in detail), although there are some valid frontend use cases that are remniscient to how we implement DDD on the backend.

Which of approach we can use for Domain-Driven Design?

Domain-Driven Design is a concept introduced by a programmer Eric Evans in 2004 in his book Domain-Driven Design: Tackling Complexity in Heart of Software. It is an approach for architecting software design by looking at software in top-down approach.

Should domain objects have interfaces?

Show activity on this post. My feeling on this is that domain objects (not domain entities, as that title implies something to do with a database) should not be interfaces unless you have a very compelling reason to believe that you will need to support multiple implementations at some point in the future.


1 Answers

I didn't really start to understand why or how you would decouple the domain model from presentation concerns until I started following the work of Greg Young and Udi Dahan (via Martin Fowler).

They have been teaching a principle known as Command and Query Responsibility Segregation (CQRS).

My interpretation of CQRS is that there are two sets of responsibilities that can pull a domain model in different directions, resulting in a model that does a mediocre job of both. The two responsibilities are commands (i.e. behavior that would cause a write to the database) and queries (i.e. reading from the database in order to fetch data for UI consumption). An example would be adding getters and setters to your entities to support databinding in the UI. If your model has getters and setters, it will probably do a poor job of modeling state changes that need to happen transactionally or encapsulating business logic. It will also have no way of modeling the business context of state changes (see Event Sourcing).

In DDD terms, you might say that the domain model and the presentation model are usually in separate Bounded Contexts.

The solution as prescribed by CQRS is to create one model for commands and another for queries. If your current model has methods for changing state (i.e. the behavior in the model), and getters that expose state to a UI for databinding, you would refactor these two responsibilities into separate models for commands and queries. The query model will not be mapped to your domain entities, but instead directly to the database. If your database doesn't capture derived state from your domain model, check out a pattern called Eager Read Derivation.

If your system is simply CRUD and has no behavior, try out a scaffolding system that can be automatically built off of your database schema, like ASP.NET Dynamic Data

like image 115
pnschofield Avatar answered Sep 21 '22 01:09

pnschofield