Editorials

Can a Database And Domain Model Be The Same?

One nice thing about having a fairly close design between you object models and your database model can be the time savings for documentation. Yes, I said documentation. I keep hearing that we don’t need class models or database relationship diagrams. Still, I have never worked on a project where diagrams were available and accurate that they weren’t often in use.

However, is it really worth the extra effort to make both models identical, or even close? Let me see if I can design a simple scenario we can all understand without a lot of work. Let’s take something as simple as an address. This can be a mailing address, a physical address, the address of an employer for an employee, the address of a favorite restaurant, etc. The point is, an address is something that may be associated with any number of entities. Businesses, people, people playing specific roles in relationship to a business or other people, etc. All of these kinds of entities may have an address associated with them, or a relationship between them and some other entity.

In a database, we tend to have separate tables for just about everything, to keep things simple. Some more sophisticated databases may use type/subtype data structures so some data may be shared. However, in general, if an entity has an address, it will have its own address data or a table for just that entity types addresses. This works great in a database. However, if we base our business objects on that same model (ala…Entity Framework) then there is a lot of redundancy that may be removed by having a couple shared objects such as an Address object, and an Addresses collection object.

You could have any number of different objects such as a person or business object, each of which may have its own instance of an address collection of addresses. You would probably want to have this kind of object model because you could create methods and properties in these addresses or address class that won’t have to be duplicated against a bunch of different classes. You could have validation business rules, search methods, build address properties as expressions based on other properties, such as City, State Zip Code address lines as used in the USA.

So, where do you want to put your effort? Do you want to take the time to adapt your data from your domain model to fit your database model? Do you want to make your database model the same as your domain model? Do you want to make your domain model the same as your database model? All of these options have pros and cons. Maybe you just want to bypass the Dissonance of Objects to Relational and use an object storage engine instead?

The really fun part of this whole line of questioning is that, once again, it depends. I know this is a controversial topic, and perhaps I am trolling for comments. But I think it is always healthy to explore again why it is we do things. It’s always the people who remember when things blew up because they took one path or another, and the pain that it can bring, that help us bite the bullet and do the right work for the right reasons.

Cheers,

Ben