JOB REFERRALS
    ON THIS PAGE
    ARCHIVES
    CATEGORIES
    BLOGROLL
    LINKS
    SEARCH
    MY BOOKS
    DISCLAIMER
 
 Friday, March 02, 2012
Leveling up “DDD”

Eric Evans, a number of years ago, wrote a book on “Domain Driven Design”.

Around the same time, Martin Fowler coined the “Rich Domain Model” pattern.

Ever since then, people have been going bat-shit nutso over building these large domain object models, then twisting and contorting them in all these various ways to make them work across different contexts—across tiers, for example, and into databases, and so on. It created a cottage industry of infrastructure tools, toolkits, libraries and frameworks, all designed somehow to make your objects less twisted and more usable and less tightly-coupled to infrastructure (I’ll pause for a moment to let you think about the absurdity of that—infrastructure designed to reduce coupling to other infrastructure—before we go on), and so on.

All the time, though, we were shying away from really taking the plunge, and thinking about domain entities in domain terms.

Jessica Kerr nails it, on the head. Her post is in the context of Java (with, ironically, some F# thrown in for clarity), but the fact is, the Java parts could’ve been written in C# or C++ and the discussion would be the exact same.

To think about building domain objects, if you are really looking to build a domain model, means to think beyond the implementation language you’re building them in. That means you have to stop thinking in terms of “Strings” and “ints”, but in terms of “FirstName” and “Age” types. Ironically, Java is ill-suited as a language to support this. C# is not great about this, but it is easier than Java. C++, ironically, may be best suited for this, given the ease with which we can set up “aliased” types, via either the typedef or even the lowly preprocessor macro (though it hurts me to say that).

I disagree with her when she says that it’s a problem that FirstName can’t inherit from String—frankly, I hold the position that doing so would be putting too much implementation detail into FirstName then, and would hurt FirstName’s chances for evolution and enhancement—but the rest of the post is so spot-on, it’s scary.

And the really ironic thing? I remember having this conversation nearly twenty years ago, in the context of C++ at the time.

Want another mind-warping discussion around DDD and how to think about domain objects correctly? Read Allen Holub’s “Getters and Setters Considered Harmful” article of nine (!) years ago.

Read those two entries, think on them for a bit, then give it a whirl in your own projects. Or as a research spike. I think you’ll start to find a lot of that infrastructure code starting to drop away and become unnecessary. And that will let you get back to the essence of objects, and level up your DDD.

(Unfortunately, I don’t know what leveled-up DDD is called. DDD++, maybe?)


Saturday, March 03, 2012 7:12:15 AM (Pacific Standard Time, UTC-08:00)
Thanks, Ted!

Successor to DDD... maybe DaDaDa?
Saturday, March 03, 2012 9:26:52 PM (Pacific Standard Time, UTC-08:00)
Eric Evans points, however, that one should use the technology, not fight with it, when implementing DDD, and that you should use DDD focused at the core of your business, and leave the less important parts to other techniques.

I just can't imagine FirstName, LastName and Email being the core of any application. They are sequences of characters, from the user input to the database, and rarely manipulated besides simple validation and trimming. There's simply no reason to make them first class citizens in your domain model. This just remembers me of university colleagues of mine who used to over-engineering everything, creating different classes for X, Y and Z components for polygon vertexes instead of simply using double.

All this just sounds like the purists' chant. If you keep it too 'pure', DDD is only really possible with an imaginary language, on an imaginary platform, using imaginary technologies.

Keep it simple, keep it real.
T
Sunday, March 04, 2012 7:49:12 AM (Pacific Standard Time, UTC-08:00)
I don't think any of what you said is in conflict with what the DDD books says or whats Eric Evans has ever said. I imagine that he would agree wholeheartedly. But what people do with it is another thing!
Zubies
Sunday, March 04, 2012 11:43:44 PM (Pacific Standard Time, UTC-08:00)
I beg to differ. Java isn't ill-suited to this kind of programming. It just requires some ahead-thinking to have robust building blocks. Those would be:
- An abstract super-class for string-based types (basically equals, hashCode and toString)
- Validation rules, which you can pass to this super-class, so you only ever have valid value objects
- A generic converter from and to String for the ORM (UserType for Hibernate).
- A generic converter from and to String for the web framework. Spring MVC 3 and JSF support locale-dependent conversion, which makes life much easier.
- An abstract super-class for number-based value types, which inherits from java.lang.Number. This lets number-formatting work out of the box.

We did so and our value types were one-liners most of the time and we could use them end-to-end without hassle. We only had Java 1.4 at the time, otherwise we'd used the generic builder pattern with all its amenities.
@T: And then DDD became very real in Java. In fact, the code was so readable that our customer was able to understand what we were doing in it.
Frisian
Tuesday, March 06, 2012 11:37:45 AM (Pacific Standard Time, UTC-08:00)
I agree with other commenters that wrapping such types in a wrapper class often helps code clarity. It can also catch bugs, for example if you swap a FirstName and a LastName.

Syntactically, this approach works better in language that have something like a Scala case class or a functional-programming ADT. Then you can write FirstName("John"), which amounts to prefixing the string by the type.
Tuesday, March 06, 2012 11:55:36 AM (Pacific Standard Time, UTC-08:00)
Err, I see that the comments on the linked blog entry supersede mine. Never mind.
Sunday, March 18, 2012 12:18:14 PM (Pacific Daylight Time, UTC-07:00)
Hi Ted,

Now and then I listen to an old recording you did together with Don Box and Mark Pollack for .NET Rocks where you talked about object/relational mapping as the Vietnam for the software industry. 8 years has gone since then and yee you were right. I keep smiling each time I listen to it and also when the two vets explained reference leaking to a then young, excited Buckaroo.

Classic quotes ...

Ted Neward: So, then the question that he was asking is, how do you prevent me from returning ‘this’ from inside the object?
Mark Pollack: ...you could potentially do that, I suppose.
Ted Neward: So, I guess the answer Don is, don’t do that.

http://www.dotnetrocks.com/default.aspx?showNum=89
Jonas Ekström
Sunday, March 18, 2012 12:30:49 PM (Pacific Daylight Time, UTC-07:00)
Correction the show was 5 years ago. I guess time just fly when you work on your enterprise wide domain model ...

Excuse me I just had to share another quote from this the same show:

Carl Franklin: But, this industry of course has a history of innovating ourselves out of jobs, you know what I mean?
Don Box: No, I completely disagree, Carl.
Carl Franklin: Do you really?
Don Box: Yeah.
Carl Franklin: Ok.
Don Box: I mean, we basically just reinvent the same crap every ten years.
Jonas Ekström
Comments are closed.