[C# Design Patterns] Data Mapper and Active Record

This article will cover two design patterns – the Data Mapper pattern and the Active Record pattern. These are two slightly different techniques you can use to let your programs interact with databases.

 

Video version here

 

What do these patterns do?

Both of these patterns are ways to convert between business objects (classes) and a database records.

 

Examples

For this example, we’ll use a Customer class as the business object. This is a very simple class, with only three properties, to keep the demo code small.

 

 

With the Data Mapper design pattern, your business object classes do not have any way to directly communicate with the database. If you want to create a Customer object from a database row, or save a Customer object to the database, you need to call a separate class. That other class is the data mapper.

 

How to implement the Data Mapper design pattern

Here is how you might build a data mapper for this Customer object.

 

 

The Save and Delete methods need to receive the Customer object (or, at least the ID from it) in order to work. The Save method needs the Customer object to insert, or update, its property values into the database. The Delete method needs the ID, to know which record to delete in the database.

 

How to implement the Active Record design pattern

The code below is a Customer class that uses the Active Record pattern.

 

 

The code is almost the same as what was in the Data Mapper class. The only significant difference is that you don’t need to pass the Customer object to the database methods – it’s already available, since those methods are inside the Customer object.

 

When to use the Data Mapper or Active Record design pattern

This is mostly a decision based on your preference.

The Data Mapper is nice because it separates the mapping from the business object class – which follows the Single Responsibility Principle. However, if you ever modify your business class, you would need to remember to also modify the data wrapper class.

When you use the Active Record pattern, it’s slightly quicker to update the mapping, if you change the business class – because the mapping code is in the same class. However, it makes your business classes larger.

My personal preference is for the Data Mapper pattern. I try to put a lot of “business logic” in my business classes. I don’t want to make them even longer, with the extra database methods.

 

Whichever pattern you choose, use that pattern for every object in your project. Being consistent will make it easier for you to modify your program, and for other people to use your code.

Leave a Reply

Your email address will not be published. Required fields are marked *