views:

1055

answers:

8

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.

+6  A: 

If you're doing an MVC pattern, you need view objects; the DDD is just your model. That doesn't mean you must always use MVC; a DDD could be built, say, as a simulator, where all you look at is log messages emitted. But in MVC you really should have separate view objects.

Now, ask yourself why that would be? The answer is that the view can change, even though the business model doesn't. The DDD model should express, in the business's terms, what is essential to the business.

Charlie Martin
+2  A: 

Within an MVC design you would typically have a mapping from Repository -> Domain Models and then from Domain Models -> View Models. The View Models often contain Domain Objects though.

Todd Smith
+4  A: 

DDD is a way of thinking while designing a software that starts with modelling the domain. As the webpage puts it:

Domain-driven design is not a technology or a methodology. It is a way of thinking and a set of priorities, aimed at accelerating software projects that have to deal with complicated domains.

One thing that follows naturally out of this design pattern is a layered architecture. As it is said in DDD Pattern Summaries

Partition a complex program into LAYERS. Develop a design within each LAYER that is cohesive and that depends only on the layers below. Follow standard architectural patterns to provide loose coupling to the layers above. Concentrate all the code related to the domain model in one layer and isolate it from the user interface, application, and infrastructure code. The domain objects, free of the responsibility of displaying themselves, storing themselves, managing application tasks, and so forth, can be focused on expressing the domain model. This allows a model to evolve to be rich enough and clear enough to capture essential business knowledge and put it to work.

Whether you need to have display objects to do that? That is just one way of implementing this, but might not even be the best to provide loose coupling. As for an example: maybe the view layer is but a webbrowser and xlt files to visualize xml files emmitted by the business layer? If anybody has more fitting examples, please add them. My point is that DDD stresses a layered architecture, but does not limit this to one possible solution.

Ralph Rickenbach
Excellent answer, bravo
Mark Rogers
A: 

Check this out for an (another) answer. :)

Arnis L.
+1  A: 

The answer is quite straight :

  • The domain objects have domain oriented design and methods.
  • The view objects have view/control oriented design and methods.

If you can use your domain objects in the view, they are maybe not quite domain oriented.

Think Before Coding
A: 

Please see my response below which would address your question too.

http://stackoverflow.com/questions/549305/how-to-handle-communication-between-the-domain-and-database-layers/1209765#1209765

jeyoung
Well, it's almost always easy to get people to agree to create separation with the data layer, but with presentation layer? I think it's a different topic.
Mark Rogers
The separation between the business and presentation layers is more implicit. By using controllers (in MVC) or code-behind, you are aleady using a layer of indirection; in other words, the code-behind is the glue (application) code between presentation and businness layers.
jeyoung
+1  A: 

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

pnschofield
This is a really really good answer. One additional thing to point out, which is relevant to the question, is that a system should be architected in layers, and you don't want to expose your domain objects (the internals of business logic layer) to your UI (the client layer).
Joseph Daigle
A: 

Domain objects are really the internal logic inside your business logic layer. They shouldn't be directly exposed to your clients (UI layer). Instead, encapsulate the usage of your domain model into application services. The application services can utilize lightweight DTOs and/or command objects to pass data and intent between the client and the domain model.

Joseph Daigle