Using Domain Driven Design with a Three Tiered Architecture

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

One of the questions I received related to the topic of Domain Driven Design was related to how we implement DDD in a three tiered architecture. The key to this is with the Repositories and Factories.

Let’s take for instance a website that consists of a web front end, a middle tier, and a database. The customer comes to the website and saves an order. The web front end will create the Order entity and populate it with the information submitted by the user. It will then call the RepositoryFactory to determine what Repository to use to save the Order. In the config file, it is specified that MiddleTierRepository should be used to implement IOrderRepository. So, the Factory returns the MiddleTierRepository and the web front end calls SaveOrder on it. This Repository calls a web method in the middle tier called SaveOrder.


The middle tier then receives the Order. It performs an type of validation upon it and decides to save it. It calls the same RepositoryFactory used above, but in its config file it is specified that SqlServerOrderRepository should be used in this instance to save the Order. So, the Factory returns the SqlServerOrderRepository, which also implements IOrderRepository, and calls SaveOrder on it to save it to the database.


We do use an ORM tool in this environment. We use nHibernate. So, in the situation above, we would use NHibernateOrderRepository instead of SqlSeverOrderRepository for the middle tier to save the order to the database.

In a nutshell, that is how we translated these patterns into a three tiered architecture.

The Long Fall Back to Earth

I’ve been a fan of Jars of Clay ever since college, but their last few albums have been very exciting. Their latest, ‘The Long Fall Back to Earth’, released today and it is very, very good. You have to check it out. If you want to try only one song from it, I’d recommend Closer (which was released a few months ago on an EP) or The Long Fall. But every song on the album (there are 14 of them!) is awesome.

Updated 4/22/09: So, the more I listen to this the more I’m struck by different songs.  “Hero” and “Weapons” are both must listens.  “Headphones” is pretty much the only song I could do without.  It sounds like he wrote “Boys” for his kids, which as a father really strikes me.   Anyway, just wanted to add a couple of the songs I listed above.

Fire your cable company!

In these challenging economic times, everyone is looking for some ways to save money. Well, I have an idea that will save a ton: Fire your cable company!

Here’s the deal. I have not had cable TV for at least 7 or 8 years now, and I don’t miss it at all. I have never missed an episode of Lost or Battlestar Galactica. Yes, I know Battlestar Galactica was on the Sci Fi channel. But I saw every episode without paying the cable company $50+ per month for the privilege of watching their content.

So, how do you fire your cable company and yet still get to watch high quality content for all of your favorite TV shows? There are a number of options.

Online Streaming

Online Streaming is going to be your cheapest option. If you have a computer and have broadband internet, you can stream almost every television show for free. Hulu is the best place to find content, but all networks are not represented there, so you can go direction to the network’s website as well. Normally shows are posted online the day after they air, but sometimes you have to wait a week (as in the case of the final couple of Battlestar Galactica episodes). ABC’s streaming player is particularly good, for all of my fellow Lost fans out there. You do have to wait through commercials with this option, but there are normally one 3 or 4 and they are very short.


If you can’t find a show available for online streaming, you don’t want to wait, or you want to be able to watch the show disconnected from the internet, iTunes is a good option. Using iTunes, you can download standard definition episodes for $1.99 and HD for $2.99. You can also buy season passes for shows that will save you some money. This is a great solution if you want to watch while on an airplane, on an iPhone, or other portable device. There are no commercials to watch if you use this solution.


The above solutions all have you watching television shows on your computer. But, you can also watch this content on your TV. That’s where a Home Theater PC, or HTPC, comes in to play. I wrote a post a while ago about my HTPC. I took an old Dell laptop and plugged it into my HD TV. This is great for most content, but sometimes I run into problems with that older PC not having enough horsepower for rendering the video. I’m hoping to upgrade this soon with a new Mac Mini or Dell Studio Hybrid. If you use a system like this you have the freedom of watching online streaming content on your TV. Adding a TV tuner to the computer will give you a DVR that you don’t have to pay a monthly fee for.

NetFlix or

TV show come out on DVD normally a couple of months at the conclusion of a season. You can buy the DVDs, but normally I don’t watch a given season of a show enough to justify buying it. Using a service like Netflix or is a great solution. Jenny and I have watched several seasons this way and it worked great. Each disc normally has 4 episodes on it, so we’d watch a disc over the course of a week. When we were done, we’d put it in the mail and get the next one a day or 2 later. We could watch an entire season of a show in 6 weeks this way. We watched all 7 seasons of Gilmore Girls in the course of a summer a couple years ago doing this.

HD Antenna

None of the solutions I have outlined above will work for sporting events, local news and weather, or my daughter’s morning cartoons. When we moved into our new house I installed an HD antenna in the attic. I took the output from this antenna and tied it into the coax running throughout the house by finding where the cable company’s line comes in, unplugging it, and attaching the cable from the antenna. I now have HD content coming to the entire house without having to pay a cable company for it. This is only going to be local broadcast stations, but that’s more than we normally watch. Plug this into the TV tuner built into your HTPC (above) and you have an HD DVR.


So, as you can tell, there are a lot of alternatives to play a ton of money to a cable company to receive television content. I use all of the options above from time to time with great success. I never miss a show that I want to watch and I don’t have to play a huge monthly fee for it.

Domain Driven Design Part 3: Repositories and Factories

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 I conclude my series on Domain Driven Design. In the first part we looked at a brief introduction and the concept of Entities. In the second part we covered Value Objects and Services. Today we’ll look at Repositories and Factories.

Repositories and Factories

Eventually, when defining your Domain language, you are going to have to interact with the data stores and/or back end systems. This is where Repositories come into the picture. A Repository is meant to translate the data from the user identifiable Domain Language into the format needed for the data store, and then to perform the operations needed to send it there.

I will create an interface for each Entity that needs to be stored using a repositories. So, we have a Reservation Entity, I’ll have an interface called the IReservationRepository. It will define all of the CRUD operations. Then, for each datastore, I’ll create a repository class that implements one or more of these interfaces. We may have the SqlServerRepository that implements IReservationRepository, ILocationRepository, etc. The piece that ties this together is the Factory.

I’ll create a factory called the RepositoryFactory that has a method called GetReservationRepository that returns an IReservationRepository. It looks in a configuration file to get the correct class (the SqlServerRepository) that needs to be created and returned to be used as the IReservaitonRepository.

So, what does all of this buy us? Let’s say that today you are storing your reservations in a database, but tomorrow you need to store them in some other type of back end system. You create a new repository implementing the interface and modify your configuration file and you are up and running with the now data store. Or, if you want to write unit tests using a mock repository that doesn’t hit an actual database that is easy to do as well.


So, this has been a brief overview of Domain Driven Design. I have found this to be a very useful tool when designing systems as it fits in cleanly with the rest of our user-centric methodology and gives you a concise framework that can be used to architect anything from the most simple application to the very complex.

There are two books that I would recommend for those that want to dig deeper into the subject. Those books are:

Notes on Anathem

So, I finished Neil Stephenson’s book Anathem several weeks ago and have been wanting to write something about it, but I’ve struggled over where to begin. This is a very large book, and can be difficult to get through at times, but in the end I found it to be a very satisfying journey.

First I’ll talk about the world. The vastness of the world he has created and the detail he goes into to describe it rivals that of Tolkien. It takes place in a world that is almost, but not quite, ours. While reading the book you are educated on much of this world’s history to understand how it got to the state it which we find the characters.

The protagonist is a man named Erasmas who lives in the concent of Saunt Edhar. This is much like a monastery but where the god who is studies and worshiped is math and science. Stephenson has created an entire new dictionary that he uses when telling this story. But learning the terminology is not too hard of a challenge as each new chapter begins with a new definition. I’m sure the print version of the book has a glossary, but since I was listening to the audio version, I did not have this tool. I became so familiar with the terms as I was reading the book that I began to think of my iPhone as a “Jeejah”.

Much of this book reads like a metaphysical lecture. The characters must dive into the depths of mathematics, astronomy, and philosophy to solve the mystery of an seeming alien ship that has presented itself. And the journey is fascinating.

I don’t want to go any farther for fear of spoiling the book. But, if you are a fan of hard science fiction, and the size and depth of the book does not scare you away, I highly recommend reading this.