Start Using More Value Objects!
At QCon London 2009, Dan Bergh Johnsson had a talk on the DDD track about how to make use of Value Objects (VO’s) (The Power of Value – Power Use of Value Objects in Domain Driven Design (PDF)). In his talk he took a piece of code and refactored it into a better piece of code by using VO’s. By doing this Dan showed some really good concepts of using VO’s. Even though I knew about the concept of VO’s before his talk, he made me realize many new scenarios where they are applicable. And more importantly that it is not limited to DDD.
So what is a VO anyway? Is it related to value type in .NET?
To answer the last question first: The real answer is no, but the way I see it there is a relation. A value type in .NET is everything that is not an object. Typically simple types like int, char, double, but also enums and structs. The relation is that often you find yourself creating VO’s where value types was previously used. It is however not limited to value types.
Now to the first question. I first came across VO’s after reading Evans DDD book (commonly known as the blue book). In DDD a Value Object is an object without an Id and are usually immutable. In examples of VO’s, Address is commonly used, which I don’t find particularly useful. I think Address way too often has an Id and people have a hard time relating to that as a Value Object. I find the Amount example I gave in an earlier post to a better example or the one below illustrated as an interface (borrowed from Dan):
public interface IPhoneNumber {bool IsValid {get;}string Number {get;}string AreaCode {get;}}
How many times have you used the concept of a phone number in your code? Like mobile, fax, fixed etc… How often have you kept it as a string? How much code do you have for doing different types of validation, getting area code etc? Do you have it all in one place or scattered around in different places? Other examples are Money, ZipCode etc… These are great candidates for Value Objects.
Now that we know what Value Objects are, what is so powerful about them and why should I use them more? Great question :-) Below is my three reasons:
- Code readability – I find that using Value Objects greatly improve the overall code readability and eliminates comments. Just like good names on methods and properties does.
- DRY (Don’t Repeat Yourself) – By having a Value Object for PhoneNumber, you avoid writing code to handle specific cases around phone numbers and you reuse them wherever the concept of a phone number is used.
- Helps people getting back into OO thinking – We who have been living in the Microsoft .NET world for a long time have been thought to think of programming from a data/database perspective and not object oriented programming as we once learned it. In other words: think of behavior instead of data.
When I started to use VO’s, I soon discovered I used them all over. They really add great value to my code. As an example, lets implement the IPhoneNumber above:
public class PhoneNumber : IPhoneNumber{private readonly string _phoneNumber;public PhoneNumber(string phoneNumber){_phoneNumber = phoneNumber;}public bool IsValid{get { return Regex.IsMatch(_phoneNumber, @"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$"); }}public string Number{get { return _phoneNumber; }}public string AreaCode{get{return Regex.Match(_phoneNumber, @"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})").ToString();}}}
The above code handles US phone numbers. To be used in a production system there should probably be even more validation in the regexp’s, but I hope the example gives you and idea of what I’m thinking.
