BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Domain-Driven Design Case Study: So We Thought We Knew Money

| by Jonathan Allen on Feb 22, 2007. Estimated reading time: 2 minutes |

In a new case study published by Domain Language, Inc., Ying Hu and Sam Peng show how they solved some major problems dealing with international currency by selectively applying Domain-Driven Design to their existing application.

The first thing Hu and Peng did was create Currency and Money classes. The first wraps a string and ensures said string is actually a valid currency. The second merges a currency with the amount of that currency.

Pretty typical stuff, but what makes this study interesting is how they thread the new class into their existing code. In one example they only change the external interface, within the class they still use a string and decimal to store the values.

But wait a second, the legacy settlementAmount and settlementCurrency still exist inside Order as private members. Isn’t it better to replace them with Money too? Yes, ideally, all private members should be replaced too. They are still there because of the shortage of time: due to the massive usage of primitive types, we found it impractical to exhaustively replace every occurrence. As a compromise, we decided to refactor important interfaces first. This way, even though many of the ugly bits are still around, they are hidden within a well defined boundary.

No case study would be complete without mentioning some of the problems they ran into. The first issue they come across is feature creep within a class.

This is only the start. These value objects are now widely used in the system. But because of their prominent expressive power, developers start to put stuff in just because of convenience.  Consider the following method defined inside Rate class:

This method adds the “moneyToAdd” to the “target” and makes sure the resulting Money object is expressed in the target Money object’s Currency. Whoever put it in probably wanted to encapsulate the conversion between two Money objects, and perhaps some form of this function would have made sense there. However, adding money certainly is not Rate’s responsibility, and so this new function weakened Rate’s representation of the rate concept and at the same time subtly coupled it to a higher layer of the system that adds Money for a particular reason.

Another issue we deal with is the unclear message sent by the fact that a fair amount of
legacy primitive types still stick around side-by-side with their value object counterparts.
Instead of refactoring, some developers continue using them. They have even added
operator overloading to convert value objects back to primitive types!

Hu and Peng conclude on a positive note.

And yet, in spite of all the incompleteness and imperfection of our ongoing development work, these "domain fundamentals" have raised our level of abstraction above the level of decimals and strings, and have been one of the keys to allowing our system to continue to develop and produce rich applications. Our domain model is evolving, and so is our domain- driven design journey. 

Do you have your own stories about introducing Domain-Driven Design into an existing project? Let us know.

 

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and dont miss out on content that matters to you

BT