Domain Driven Design Part 2: Value Objects and Services

Note: This is part of my series on Domain Driven Design:

  1. Part 1: Introduction and Entities
  2. Part 2: Value Objects and Services
  3. Part 3: Repositories and Factories
  4. Using Domain Driven Design with a Three Tiered Architecture

Today’s post is a continuation of my series on Domain Driven Design. One of my favorite parts of Domain Driven Design is how it narrows your focus down to a handful of design patters that will work for just about any application you are building. I discussed the first of these design patters in the last post, the Entity. This is the most important pattern for building out your ubiquitous domain language. The next pattern to discuss is the Value Object.

Value Objects

Just like Entities, Value Objects have attributes. The difference between them comes in the way that you identify them. An Entity has a unique ID and is identified by the ID. For example, an employee has an Employee ID number. A Reservation, as we discussed in the last post, has a Reservation Number. Entities can always be identified by a unique ID.

Value Objects, on the other hand, contain a collection of attributes, and it is those attributes that define the object. Value Objects are immutable, that is they cannot be modified after they are created.

Examples of Value Objects in the car rental application would be the pickup date / time. This date / time object would have a month, day, year, hour and minute associated with it. There is no separate unique ID to identify it as it is defined by the the collection of its attributes. If you modify one of those attributes, it is a completely separate date / time object. On the other hand, if you modify one of the attributes of a Reservation (for example, the Car), you have modified the Reservation but the reservation itself is the same.

So, the easiest way to think of the separation between Entities and Value Objects is to determine if it has a unique ID or if it’s identity is fully defined by the sum of it’s attributes.

An interesting thing to consider is the Rate object that I discussed in the last post. A Rate has a price, a location, and a vehicle. Is this an Entity or a Value Object? Normally I would probably say that it is a Value Object, because if you change one of those attributes, you have changed the quoted Rate. However, having worked at a rental car company, I know that they assign IDs to quoted rates and store every quoted rate in the database. So it is persisted indefinitely and is referred to by the unique ID. So, I believe a case could be made for it either way.

Services

So far we have talked about methods that are applied directly to Entities or Value Objects. But, what if you have a method that does not naturally fit there? An example would be a GetRates that takes the pickup location, pickup date and return date, and returns a list of available rates. Where does this method go? This is where a Service comes in. You would create a RatesService that contains this method.

An important characteristic of services is that they are stateless. That is, the RatesService takes some values as input, performs an operation, returns a result, and then goes away. This would be a good candidate for static methods in most languages.

In Domain Driven Design these services are treated as first class citizens along side the Entities as you are mapping out your domain language. Users can clearly think of a RatesService that you throw information into and gives you meaningful results.

In the next post I will discuss the last two design patterns used in Domain Driven Design: Repositories and Factories.

Advertisements

Rue Royale is certainly something to write home about

Rue Royale

Back in September of 2007, I had a post talking about a new band that I had found named Rue Royale. They are a husband and wife duo that features beautiful smooth melodies, acoustic guitars, and artistic lyrics. A couple of weeks ago my iPod came across them again while I was listening to a random playlist, and was started wondering what they were now up to. Not only did I discover that they had a full length album out now, but it has been out for over a year!

The self-titled album includes 10 songs, three of which are from the EP – Parachutes and Lifeboats, Even in the Darkness, and U.F.O. You really ought to check out their MySpace page and, if you like the music, pick up the album. This quickly replaced the new U2 album at the top of my playlist.

Domain Driven Design Part 1: Introduction and Entities

Note: This is part of my series on Domain Driven Design:

  1. Part 1: Introduction and Entities
  2. Part 2: Value Objects and Services
  3. Part 3: Repositories and Factories
  4. Using Domain Driven Design with a Three Tiered Architecture


At StoneHenge Partners, we’ve realized that the best way to ensure the success of software development projects is to keep the focus on the users and their experience at all times. Too many times, once you get into the code, your focus shifts to the underlying technologies and the back end systems and you completely forget about those users that you are developing for.

There are several aspects of our software development methodology that allow us to keep this focus. We always have a User Experience Architect assigned to the project who’s only focus is the interaction of the user and making sure that the end project will satisfy their needs. We size, scope, and track projects user Function Point Analysis where you are solely focused on end user identifiable transactions and files. We do iterative development so that the end users sees progress on the application as soon as possible and can submit feedback. But, another aspect of our methodology that allows us to stay focused on the users is Domain Driven Design.

In Domain Driven Design, you start your application architecture by defining the ubiquitous domain language. You map out the domain objects as the user would see them. I worked for a number of years in the car rental industry, so I’ll use some examples from there to flesh these ideas out.

On a car rental website you have a collection of rates. Each rate has a car, location, pickup date, and return date associated with it. So, in our domain language we would map out what a Rate is. We’d follow this up by defining a Car and a Location. These are three Entities. What attributes come to a users mind when they think of these entities? What operations can be performed upon them?

When a user has chosen a Rate, they will book it. So, an operation on the Rate entity may be Book(). The result of a user booking a Rate would be a Reservation. So, this is another entity for our model. What attributes are associated with a Reservation? What operations?

Once you have walked completely through this process, you end up with your Domain completely mapped out. This is your ubiquitous language that will be used to describe everything that happens in the application.

The difference with Domain Driven Design is that these domain objects that are defined, these entities, feed directly into the code that is written. So, we have defined an entity called a Rate. We therefore have a class called a Rate. This has the properties of Car, Location, pickup date, and return date that we discussed. This also has a method called Book that returns a Reservation. At this level of the application code we are not thinking at all about how the data is going to be stored or communicated to the back end applications. We are thinking completely about the domain objects that the user would identify and understand.

So, there are five main patterns used in Domain Driven Design. These are Entities, Value Objects, Repositories, Services, and Factories. I have briefly discussed Entities in this post. Part 2 will continue with a discussion of Value Objects and the difference between these and Entities.

U2’s new album is Magnificent

I’m a pretty big fan of U2, and as such, I had to pick up their album, No Line on the Horizon last Tuesday as soon as it came out. Actually, my morning was like this: get up, shower, get dressed, open the computer, download the album from Amazon. That was especially cool since Amazon had it for only four dollars!

So, how is the new album. I love it! I’ve been listening to it almost constantly since Tuesday during the periods of time that I have been in my office. I enjoy it more and more with each listen. Two songs in particular stand out to me: “Magnificent” and “Moment of Surrender”. The title song, “No Line on the Horizon” and their first single “Get on Your Boots”, I would have to say are actually two of the weakest songs on the album.

Musically, this album strikes me as being more similar to some of their older stuff, like Achtung Baby, than the more recent ones. Lyrically, it’s hard to miss the religious overtones. Being a Christian myself, I’ve always loved that facet of their music. You feel like you are in the middle of a worship service lead by one of the greatest rock bands ever. You just can’t beat that!

So, I would highly recommend this album. It looks like you can still pick up the MP3 version of it for $3.99 from Amazon here.

So, where have you been?

emmaatparkOk, I’ve gotten really bad at keeping up with the blog.  Work and family have both been extremely busy lately.  I’ve been posting pretty regularly on Twitter and Facebook, but nothing here for quite a while.  So, what have we been up to?

Well, the last post was in December. We, of course, had Christmas and the kids had a blast.  Emma opened all of her presents, all of Christopher’s, all of mine, and all of Jenny’s!  Needless to say she thinks Christmas is about the greatest thing possible.  We were able to spend a lot of time with friends and family, which is always nice.

So, Christopher and Emma have both been growing like weeds (or flowers, as Jenny says). Christopher is walking all over the place trying to do everythign Emma does and Emma is trying to keep him away from everything she’s trying to play with.  Makes for an interesting dynamic!  We are beginning to look at preschools for Emma for next year.  I have a feeling we are about to enter a whole new stage in life as they begin to go to school.

Work as been really busy, which is especially good in this economy.  We are constantly bidding on more and more projects which should keep us very busy for the rest of the year. I’ve very excited about where the company is headed.

So, I’m going to do my best to post here more regularly.  I’m currently working on reviews for U2’s new album and Anathem by Neil Stephenson, both of which are remarkable.  So, keep watching.  There will be more to come!