CodeProject 中有一个不错但简单的演示。值得通过它。新手可以对设计 DTO 有一个基本的了解。
http://www.codeproject.com/Articles/8824/C-Data-Transfer-Object
以下是内容摘要:
数据传输对象“DTO”是一个简单的可序列化对象,用于跨应用程序的多个层传输数据。DTO 中包含的字段通常是原始类型,例如字符串、布尔值等。其他 DTO 可能包含或聚合在 DTO 中。例如,您可能有一个包含在 LibraryDTO 中的 BookDTO 集合。我创建了一个由多个应用程序使用的框架,该框架利用 DTO 跨层传输数据。该框架还依赖于其他 OO 模式,例如 Factory、Facade 等。与 DataSet 相比,DTO 的一大优点是 DTO 不必直接匹配数据表或视图。DTO 可以聚合来自另一个 DTO 的字段
这是所有数据传输对象的基类。
using System;
namespace DEMO.Common
{
/// This is the base class for all DataTransferObjects.
public abstract class DTO
{
public DTO()
{
}
}
}
这是 DTO 的派生类:
using System;
using System.Xml.Serialization;
using DEMO.Common;
namespace DEMO.DemoDataTransferObjects
{
public class DemoDTO : DTO
{
// Variables encapsulated by class (private).
private string demoId = "";
private string demoName = "";
private string demoProgrammer = "";
public DemoDTO()
{
}
///Public access to the DemoId field.
///String
[XmlElement(IsNullable=true)]
public string DemoId
{
get
{
return this.demoId;
}
set
{
this.demoId = value;
}
}
///Public access to the DemoId field.
///String
[XmlElement(IsNullable=true)]
public string DemoName
{
get
{
return this.demoName;
}
set
{
this.demoName = value;
}
}
///Public access to the DemoId field.
///String
[XmlElement(IsNullable=true)]
public string DemoProgrammer
{
get
{
return this.demoProgrammer;
}
set
{
this.demoProgrammer = value;
}
}
}
这是 DTO 的助手类。它具有序列化和反序列化 DTO 的公共方法。
using System;
using System.Xml.Serialization;
using System.IO;
namespace DEMO.Common
{
public class DTOSerializerHelper
{
public DTOSerializerHelper()
{
}
///
/// Creates xml string from given dto.
///
/// DTO
/// XML
public static string SerializeDTO(DTO dto)
{
try
{
XmlSerializer xmlSer = new XmlSerializer(dto.GetType());
StringWriter sWriter = new StringWriter();
// Serialize the dto to xml.
xmlSer.Serialize(sWriter, dto);
// Return the string of xml.
return sWriter.ToString();
}
catch(Exception ex)
{
// Propogate the exception.
throw ex;
}
}
///
/// Deserializes the xml into a specified data transfer object.
///
/// string of xml
/// type of dto
/// DTO
public static DTO DeserializeXml(string xml, DTO dto)
{
try
{
XmlSerializer xmlSer = new XmlSerializer(dto.GetType());
// Read the XML.
StringReader sReader = new StringReader(xml);
// Cast the deserialized xml to the type of dto.
DTO retDTO = (DTO)xmlSer.Deserialize(sReader);
// Return the data transfer object.
return retDTO;
}
catch(Exception ex)
{
// Propogate the exception.
throw ex;
}
}
}
现在开始序列化/反序列化:
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();
// Serialize the dto to xml.
string strXml = DTOSerializerHelper.SerializeDTO(dto);
// Write the serialized dto as xml.
Console.WriteLine("Serialized DTO");
Console.WriteLine("=======================");
Console.WriteLine("\r");
Console.WriteLine(strXml);
Console.WriteLine("\r");
// Deserialize the xml to the data transfer object.
DemoDTO desDto =
(DemoDTO) DTOSerializerHelper.DeserializeXml(strXml,
new DemoDTO());
// Write the deserialized dto values.
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;
}
}
最后这段代码在主应用程序中执行
static void Main(string[] args)
{
DemoClass dc = new DemoClass();
dc.StartDemo();
}