The DAO design pattern is traditionally associated with the Java Core J2EE Patterns, but this relatively simple pattern is equally applicable to our Microsoft .NET world.

This time I am not talking hypothetical here, since recently I saw it being used extensively in real world .NET webportal code I was auditing. I guess it is time to investigate this pattern in more detail.

data-access-object-pattern

A great advantage of the DAO pattern is that it separates the business logic and data access layers which – in an ideal situation – should know nothing about each other and where probably both will be changed frequently and independently. Changes of business logic can trust on the same DAO interface, while changes in persistence logic do not have any impact on its DAO clients as long as the interface is implemented correctly.

In this pattern the business object is the client which needs to get or set some data from a remote datasource. It is doing so by creating a data access object and a transfer object, which is a vehicle and contains the data.

The data access object contains the data source and functions like an object adapter. It fills the transfer object with data.

The transfer object just represents the data from the data source in object format.

Most of the times the data source will be some database, but it can be anything varying from a flat file to an ERP adapter. For the sake of this simplified example the data source is just another object.

The data access object design pattern is a relatively simple pattern you can use to separate you business logic from the actual implementation of the data source. The pattern is originally associated with J2EE patterns, but as I showed in the example code it also suits very well as a .NET pattern.