Introduction
The Data Transfer Object "DTO", is a simple serializable object used to transfer data across multiple layers of an application. The fields contained in the DTO are usually primitive types such as strings, boolean, etc. Other DTOs may be contained or aggregated in the DTO. For example, you may have a collection of BookDTOs contained in a LibraryDTO. I have created a framework used by multiple applications that utilizes DTOs to transfer data across tiers. The framework also relies on other OO patterns such as the Factory, Facade, etc. One of the great things about the DTO compared to a DataSet
is that the DTO does not have to directly match a data table or view. The DTO can aggregate fields from another DTO.
Using the code
- Create a project that will be common for your application.
- Generate the abstract base class
DTO
.
- Generate the Serializer Helper class.
- Create your derived DTOs that inherit from the
DTO
base class.
- Create the Console Application to test your DTO and serialization.
*** Note: All of the code is available via the downloadable zip file.***
Abstract Base Class for all Data Transfer Objects
This is the base class for all Data Transfer Objects.
using System;
namespace DEMO.Common
{
public abstract class DTO
{
public DTO()
{
}
}
}
Data Transfer Object Serializer Helper Class
This is the helper class for a DTO. It has public methods to serialize and de-serialize a DTO.
using System;
using System.Xml.Serialization;
using System.IO;
namespace DEMO.Common
{
public class DTOSerializerHelper
{
public DTOSerializerHelper()
{
}
public static string SerializeDTO(DTO dto)
{
try
{
XmlSerializer xmlSer = new XmlSerializer(dto.GetType());
StringWriter sWriter = new StringWriter();
xmlSer.Serialize(sWriter, dto);
return sWriter.ToString();
}
catch(Exception ex)
{
throw ex;
}
}
public static DTO DeserializeXml(string xml, DTO dto)
{
try
{
XmlSerializer xmlSer = new XmlSerializer(dto.GetType());
StringReader sReader = new StringReader(xml);
DTO retDTO = (DTO)xmlSer.Deserialize(sReader);
return retDTO;
}
catch(Exception ex)
{
throw ex;
}
}
}
}
Derived Data Transfer Object Example
The DemoDTO
is a derived class that inherits from the DTO
base class. This class has some simple fields defined such as demoId
, demoName
, and demoProgrammer
.
using System;
using System.Xml.Serialization;
using DEMO.Common;
namespace DEMO.DemoDataTransferObjects
{
public class DemoDTO : DTO
{
private string demoId = "";
private string demoName = "";
private string demoProgrammer = "";
public DemoDTO()
{
}
[XmlElement(IsNullable=true)]
public string DemoId
{
get
{
return this.demoId;
}
set
{
this.demoId = value;
}
}
[XmlElement(IsNullable=true)]
public string DemoName
{
get
{
return this.demoName;
}
set
{
this.demoName = value;
}
}
[XmlElement(IsNullable=true)]
public string DemoProgrammer
{
get
{
return this.demoProgrammer;
}
set
{
this.demoProgrammer = value;
}
}
}
}
Console App showing DTO example
The Console Application demonstrates how easy it is to create a DTO, serialize a DTO, and de-serialize a DTO.
using System;
using DEMO.Common;
using DEMO.DemoDataTransferObjects;
namespace DemoConsoleApplication
{
public class DemoClass
{
public DemoClass()
{
}
public void StartDemo()
{
this.ProcessDemo();
}
private void ProcessDemo()
{
DemoDTO dto = this.CreateDemoDto();
string strXml = DTOSerializerHelper.SerializeDTO(dto);
Console.WriteLine("Serialized DTO");
Console.WriteLine("=======================");
Console.WriteLine("\r");
Console.WriteLine(strXml);
Console.WriteLine("\r");
DemoDTO desDto =
(DemoDTO) DTOSerializerHelper.DeserializeXml(strXml,
new DemoDTO());
Console.WriteLine("Deseralized DTO");
Console.WriteLine("=======================");
Console.WriteLine("\r");
Console.WriteLine("DemoId : " + desDto.DemoId);
Console.WriteLine("Demo Name : " + desDto.DemoName);
Console.WriteLine("Demo Programmer: " + desDto.DemoProgrammer);
Console.WriteLine("\r");
}
private DemoDTO CreateDemoDto()
{
DemoDTO dto = new DemoDTO();
dto.DemoId = "1";
dto.DemoName = "Data Transfer Object Demonstration Program";
dto.DemoProgrammer = "Kenny Young";
return dto;
}
}
}
using System;
using DEMO.Common;
using DEMO.DemoDataTransferObjects;
namespace DemoConsoleApplication
{
class MainClass
{
[STAThread]
static void Main(string[] args)
{
DemoClass dc = new DemoClass();
dc.StartDemo();
}
}
}
Output
Conclusion
This article demonstrated a simple way to use Data Transfer Objects. These objects are a small part of a common framework developed for multiple applications. The framework has multiple layers, but one common object used throughout is the DTO. There are objects in the framework that convert a DataReader
into a Data Access Object. Once time permits, I will be posting more documents on Enterprise Architecture, and go into more detail about the framework.
References
- Patterns of Enterprise Application Architecture, Martin Fowler 2003.