3

I've got a question on my mind that has been stirring for months as I've read about DDD, patterns and many other topics of application architecture. I'm going to frame this in terms of an MVC web application but the question is, I'm sure, much broader. and it is this:  Does the adherence to domain entities  create rigidity and inefficiency in an application? 

The DDD approach makes complete sense for managing the business logic of an application and as a way of working with stakeholders. But to me it falls apart in the context of a multi-tiered application. Namely there are very few scenarios when a view needs all the data of an entity or when even two repositories have it all. In and of itself that's not bad but it means I make multiple queries returning a bunch of properties I don't need to get a few that I do. And once that is done the extraneous information either gets passed to the view or there is the overhead of discarding, merging and mapping data to a DTO or view model. I have need to generate a lot of reports and the problem seems magnified there. Each requires a unique slicing or aggregating of information that SQL can do well but repositories can't as they're expected to return full entities. It seems wasteful, honestly, and I don't want to pound a database and generate unneeded network traffic on a matter of principle. From questions like this Should the repository layer return data-transfer-objects (DTO)? it seems I'm not the only one to struggle with this question. So what's the answer to the limitations it seems to impose? 

Thanks from a new and confounded DDD-er.  

Community
  • 1
  • 1
  • 1
    I think this is a great question but I think you nailed the difference yourself - reporting vs. behavior. I worry about the maintenance burden of proliferating interfaces but I do find value in DDD for logic and straight-to-sql for reporting. – n8wrl Jun 28 '12 at 20:10
  • 1
    I think what you're asking is a great question, and it's exactly why many developers have embraces CQRS. The barrier to entry for CQRS is a little high, but not insurmountable and it has huge benefits. I would recommend these links: http://elegantcode.com/2009/11/11/cqrs-la-greg-young/, http://martinfowler.com/bliki/CQRS.html – swannee Jun 29 '12 at 22:02

4 Answers4

6

What's the real problem here? Processing business rules and querying for data are 2 very different concerns. That realization leads us to CQRS - Command-Query Responsibility Segregation. What's that? You just don't use the same model for both tasks: Domain Model is about behavior, performing business processes, handling command. And there is a separate Reporting Model used for display. In general, it can contain a table per view. These tables contains only relevant information so you can get rid of DTO, AutoMapper, etc.

How these two models synchronize? It can be done in many ways:

  • Reporting model can be built just on top of database views
  • Database replication
  • Domain model can issue events containing information about each change and they can be handled by denormalizers updating proper tables in Reporting Model
Pein
  • 1,216
  • 9
  • 16
3

as I've read about DDD, patterns and many other topics of application architecture

Domain driven design is not about patterns and architecture but about designing your code according to business domain. Instead of thinking about repositories and layers, think about problem you are trying to solve. Simplest way to "start rehabilitation" would be to rename ProductRepository to just Products.

Does the adherence to domain entities create rigidity and inefficiency in an application?

Inefficiency comes from bad modeling. [citation needed]

The DDD approach makes complete sense for managing the business logic of an application and as a way of working with stakeholders. But to me it falls apart in the context of a multi-tiered application.

Tiers aren't layers

Namely there are very few scenarios when a view needs all the data of an entity or when even two repositories have it all. In and of itself that's not bad but it means I make multiple queries returning a bunch of properties I don't need to get a few that I do.

Query that data as you wish. Do not try to box your problems into some "ready-made solutions". Instead - learn from them and apply only what's necessary to solve them.

Each requires a unique slicing or aggregating of information that SQL can do well but repositories can't as they're expected to return full entities.

http://ayende.com/blog/3955/repository-is-the-new-singleton

So what's the answer to the limitations it seems to impose?

"seems"


Btw, internet is full of things like this (I mean that sample app).
To understand what DDD is, read blue book slowly and carefully. Twice.

user229044
  • 232,980
  • 40
  • 330
  • 338
Arnis Lapsa
  • 45,880
  • 29
  • 115
  • 195
0

If you think that fully fledged DDD is too much effort for your scenario then maybe you need to take a step down and look at something closer to Active Record.

I use DDD but in my scenario I have to support multiple front-ends; a couple web sites and a WinForms app, as well as a set of services that allow interaction with other automated processes. In this case, the extra complexity is worth it. I use DTO's to transfer a representation of my data to the various presentation layers. The CPU overhead in mapping domain entities to DTO's is small - a rounding error when compared to net work calls and database calls. There is also the overhead in managing this complexity. I have mitigated this to some extent by using AutoMapper. My Repositories return fully populated domain objects. My service layer will map to/from DTO's. Here we can flatten out the domain objects, combine domain objects, etc. to produce a more tabulated representation of the data.

Dino Esposito wrote an MSDN Magazine article on this subject here - you may find this interesting.

So, I guess to answer your "Why" question - as usual, it depends on your context. DDD maybe too much effort. In which case do something simpler.

RobertMS
  • 1,153
  • 11
  • 17
0

Each requires a unique slicing or aggregating of information that SQL can do well but repositories can't as they're expected to return full entities.

Add methods to your repository to return ONLY what you want e.g. IOrderRepository.GetByCustomer It's completely OK in DDD. You may also use Query object pattern or Specification to make your repositories more generic; only remember not to use anything which is ORM-specific in interfaces of the repositories(e.g. ICriteria of NHibernate)

raminxtar
  • 11
  • 1