“Data Transfer Object” Pattern, Data Access and Web Service – Programming, Pseudocode Example, C# Programming Example
C# General

“Data Transfer Object” Pattern, Data Access and Web Service


This tutorial exposes a use case of the “Data Transfer Object” pattern design. The latter is part of a .Net solution allowing access to data via the Web Services.


For the different examples, we establish the following need:

Access to different interaction functions with the “Person” table: Add, Edit, Delete and Recover.

Columns Type Description
ID int Primary Key, Not Null
Name varchar(50) Null
Age int Not Null

Table name: “Person


Of course, a need such as it turns out to be quite simple: we could quite directly code the four methods.

Nevertheless, like any good developer we are, we still want to make the thing as scalable and generic as possible. It is within this framework that we can present the following architecture:

Figure: Web Services Architecture based on the DTO design pattern

At this level, you will say to me: “it is very beautiful the diagram but it serves what”. I propose you to continue the rest of the document, which will explain the role of each module by module.


Tip: Create a solution that contains a separate project for each component.
(project references are expressed by the double arrows of the diagram above)

Using the “Data Transfer Object” Pattern

A use of this pattern (coupled with the design pattern: “Assemble”) breaks down this need in terms of business objects under 3 distinct parts. All this in order to make it easier to use business objects corresponding to functional needs.

Domain Objects

This is a simple stream containing generic data. It exists natively in the .Net framework in the form of classes such as: DataSet, DataReader …

For our example, the class used will be “DataSet”.

Data Transfer Objects

Each class adheres to functional specifications both in terms of its fields and properties, but does not implement any business logic at the method level. Indeed, it is only a container for the transport of business information.

We will add to each DTO class the XML serialization that will eventually allow communication with any platform and any language.

Class file “Person.cs“:

PersonList.cs class file

The Assemble class

This class is the link between DTO and OD. It allows, via static methods, the conversion of a DTO to a DO and vice versa.

In this context, we can easily provide an XML feed containing DTO objects. Conversely, we can easily interpret an XML stream in DTO.


Static Class File “Assembler“:

“Business Data Access Objects” & “Data Access Objects”

“Business Data Access Objects” & “Data Access Objects”
These two components, normally distinct, are exposed here as a single component. This one takes care of all the functionalities concerning the access to the data: recovery of persons, addition / modification / suppression of a person.

File: “DataAccess.cs

As you will understand, the ideal would be to separate the business part from data access. Access to the data would only take care of connecting and executing queries on the database. Thus, we could easily change from one type of database to another (eg Sql Server, Oracle, Base Access, MySQL …) without affecting the data access business logic.

To do this, I can only advise you to use a very well known library of .Net developers: “Enterprise Library 3.1”. This integrates a component called “Data Access Application Block” which corresponds perfectly to this need. It allows the selection of the type of the database via the configuration file.

Url : http://msdn.microsoft.com/en-us/library/aa480453.aspx

Web Services

All that remains is to give users access to the data. A Web Service layer is ideal for the situation. This will interface between user and execution of requests.




So here it is, the set-up is over.

But, what are the advantages of such an architecture?

Such a project then becomes a facade of multi-platform and multi-language services. Any type of project (Windows application, website, Windows service …) can be grafted, interact and interpret very easily (XML standardization) the data, whatever the language used.

Adding a column to the table will simply add one member to the DTO class and two rows to the Assemble class.

The change of database type will only interfere with the data access part (and if the “Enterprise Library” library is used, it will show up in the only modification of the configuration file).








Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.