13

If you were to have a REST layer on top of your DDD App for CRUD, would you let the REST layer spit out domain model(in terms of data)(say for a GET)?

2 Answers 2

25

Generally, you'd want to be able to change your domain objects (for instance when you learn something new about the domain), without having to change a public interface/API to your system. Same thing the other way around: if a change is required to a public interface, you don't want to have to change your domain model.

So from this perspective I'd never expose my domain objects as-is over a public interface. Instead I'd create data transfer objects (DTO) that are part of the public interface. This way, changes to my domain and public api can change independently.

4
  • 1
    Is it possible to generate the REST part based on annotated DTOs (command, query, domain event), and if yes how? Answer it here pls: stackoverflow.com/questions/26049934/…
    – inf3rno
    Sep 29, 2014 at 19:19
  • 2
    This is a little bit biased. There is no need to artificially "guard" users from the domain in DDD: Some concepts may "go through" as near as they are modelled, others be hidden (unless the model is not quite correct). Also, in the original post "REST over DDD" is questionable. There should be (most of the time) some kind of application layer on top of domain first. This means, domain objects are already separated.
    – Roman Susi
    Jul 10, 2015 at 7:19
  • @RomanSusi I'm not sure what you mean by "guard the user from the domain"; could you elaborate a bit? I absolutely agree that the public interface will deal with the same concepts as the domain. My point is not about "abstracting" the domain away from the consumers of the public REST interface, it's about decoupling changes to the domain objects from changes to this public REST interface.
    – Marijn
    Jul 10, 2015 at 9:38
  • @Marijn Ok, maybe I read it wrong way. I just took for granted transport peculiarities/details are mostly irrelevant. However, not all changes to the domain are ok with public API, with extra abstraction or not: Only backwards compatible ones. I think, it may also be relevant how REST layer is being used. It may just bridge the frontend-backend gap, in which case there is no problem to operate in domain terms. (But usually there are frameworks for that)
    – Roman Susi
    Jul 10, 2015 at 13:14
2

You should not expose the DDD model. This is absolutely correct, because a SOA frontend should not expose implementation details to clients. Your users should depend on a business function, not an implementation detail… But this assumes a nice design of several, maybe heterogeneous, applications united into a SOA bus.

I would like to add to the answer because the mention of a CRUD interface makes me think that this could be a case of SOA abuse where SOA principles are used to glue the layers of an application, instead of a network of applications. SOA is meant as a way for the enterprise to communicate its systems, it is not a way to implement MVC! So simple yet so misunderstood. For example, just because your front end GUI uses services to access the backend you do not have a "SOA application."… what ever that means.

If this is a case of SOA used to glue layers, please revise your design and use an appropriate design architecture for that level of abstraction. Otherwise you will misinterpret the recommendations found here about no exposing the DDD model and not using CRUDY, and you will surely end up creating a separate domain model for the services interface, that then you will have to map to the DDD , which is so complicated that you will need to use dozer and things like that to map the same thing with different names, and so forth until we end up with a bloated un maintainable mess…

.. just be careful.

-Alex

Redzedi is so right that we need a clarification....

Like everything, this is quite more complicated to do than to say. Serializing a complex domain model could be so difficult that you can end up either not putting any logic in the domain, the anemic model antipattern (http://martinfowler.com/bliki/AnemicDomainModel.html), or having a separate anemic model for persistence, ie DTOs.

I don’t know what is worst, but both options are bad. You should put the logic that goes in the model in the model and you should be able to serialize directly everywhere.

In my experience using the domain model for many years, I believe that the best thing is a point in the middle. Yes, as Fowler and Evans state, business objects should carry logic, but not all (http://codebetter.com/gregyoung/2009/07/15/the-anemic-domain-model-pattern/) a little anemia with a nice service layer is best.

For example, an invoice should know about its items and have a procedure to calculate its total, which depends on the items. But an invoice's item does not need to know about invoicing. So what happens when an item changes in cost, should it have a pointer back to the father invoice as a circular reference and call the invoice's calculate total procedure?

I believe not. I think that's a task for the service layer who should received the event first and then orchestrate the procedure, with out having to couple all the business objects together for implementation purposes and violating the business interaction rules, which is what a domain model is for.

-Alex

3
  • thanks Alex, SOA or not the issue of mapping data from domain model to some DTO so that the view can consume will always remain, isn't it ?
    – redzedi
    Jun 19, 2012 at 4:24
  • 1
    Hello Redzedi, in my personal opinion I try to map the domain model directly to the database. The domain model is the data model. So I have only one business object for both the user interface and the database.
    – Alex Vaz
    Jun 19, 2012 at 14:00
  • 1
    That's a very significant thing u r saying ALex, appears very appealing to me, but i have heard all kinds of things about it , what Marijn says above is wat a lot ppl believe , but in my experience , that leads to lots of similar looking java classes in different layer where any subsequent addition leads to lots of dogmatic code mapping.
    – redzedi
    Jun 19, 2012 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.