4.业务实体的设计:
ORM实现了业务实体对象与数据表,实体属性或域与数据字段之间的对应关系。在这种条件下:一方面,可以通过Snake.Net框架的处理机制,屏蔽各种数据库差异,自动生成数据访问的SQL语句,实现对象的CRUD(即Create, Retrieve, Update, Delete方法)操作和复杂查询功能;另一方面ORM可以使数据库操变为对象操作,使OO的编程理念与数据访问完美的结合起来。
通常情况下,ORM中的每个业务实体都与一个数据表相对应,而实体内的属性或域则与数据表内的字段相对应。Snake.Net提供了DataMappingObject, DataRowObject和DataBindObject等多种基类,可以使用不同类型的数据映射技术,从而满足各种开发环境的需要。
(1)实体类的定义
A)DataMappingObject对象
DataMappingObject实现了DataTable的构架定义与业务实体的属性或域进行映射。我们以著名的示例数据库Northwind为例,根据其中的Shippers表定义一个Shipper对象。请看下面的代码。
using Eastasp.Enterprise;
using Eastasp.Enterprise.ORM;
namespace Eastasp.Samples
{
public class Shipper:DataMappingObject
{
protected string pcompanyName;
protected string pphone;
protected int pshipperID;
public Shipper():base()
{
}
public string CompanyName
{
get{return (string)GetValue(pcompanyName);}
set{pcompanyName = (string)SetValue(pcompanyName, value);}
}
public string Phone
{
get{return (string)GetValue(pphone);}
set{pphone = (string)SetValue(pphone, value);}
}
public int ShipperID
{
get{return (int)GetValue(pshipperID);}
}
}
}
B)DataRowObject对象
DataRowObject对象内置了一个DataRow变量,其公共的属性是通过获取或设置DataRow中的字段值实现的。再以Shippers为列定义一个Shipper对象。请看下面的代码。
using System.Data;
using Eastasp.Enterprise;
using Eastasp.Enterprise.ORM
namespace Eastasp.Samples
{
public class Shipper:DataRowObject
{
public Shipper():base()
{
}
public string CompanyName
{
get{return (string)GetValue("CompanyName");}
set{SetValue("CompanyName", value);}
}
public string Phone
{
get{return (string)GetValue("Phone");}
set{SetValue("Phone", value);}
}
public int ShipperID
{
get{return (int)GetValue("ShipperID");}
}
}
}
C)DataBindObject对象
DataBindObject,是通过Attribute特性,声明实现业务实体的属性或域与数据表的映射关系。再以Shippers为列定义一个Shipper对象。请看下面的代码。
using Eastasp.Enterprise;
using Eastasp.Enterprise.ORM;
namespace Eastasp.Samples
{
[TableMapping("Shippers")]
public class Shipper:DataBindObject
{
[ColumnMapping("ShipperID", IsPrimaryKey=true, AutoIncrement=true)]
protected int pshipperID;
protected string pcompanyName;
protected string pphone;
public Shipper():base()
{
}
[ColumnMapping("CompanyName")]
public string CompanyName
{
get{return pcompanyName;}
set{pcompanyName = value;}
}
[ColumnMapping("Phone")]
public string Phone
{
get{return pphone;}
set{pphone = value;}
}
public int ShipperID{
get{return pshipperID;}
}
}
}
(2)数据映射
对于不同的对象,Sanke.Net使用不同的方法进行数据映射。DataMappingObject和DataRowObject对象通过DataSet的定义文件与业务实体进行映射。首先我们在DataSet定义内引入xmlns:msprop="urn:schemas-microsoft-com:xml-msprop"命名空间(如图 4-2-1),然后使用msprop:Mapping属性将数据表映射对应的业务实体类型名称,将数据字段映射到对应的属性或域的名称。(如图 4-2-2)
<xs:schema id="orm" targetNamespace="http://tempuri.org/orm.xsd" elementFormDefault="qualified" attributeFormDefault="qualified" xmlns="http://tempuri.org/orm.xsd"
xmlns:mstns="http://tempuri.org/orm.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:msprop="urn:schemas-microsoft-com:xml-msprop">
<xs:element name="orm" msdata:IsDataSet="true">
</xs:element>
</xs:schema>
图 4-2-1 引入urn:schemas-microsoft-com:xml-msprop命名空间
<xs:element name="Shippers" msprop:Mapping="Eastasp.Samples.Shipper" msprop:Alias="Shipper">
<xs:complexType>
<xs:sequence>
<xs:element name="ShipperID" msdata:AutoIncrement="true" type="xs:int" msprop:Mapping="pshipperID" />
<xs:element name="CompanyName" type="xs:string" minOccurs="0" />
<xs:element name="Phone" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
图 4-2-2 设置数据表和字段对应的映射关系
DataBindObject对象使用TableMappingAttribute和ColumnMappingAttribute特性分别将业务实体对象和实体对象的属性和域,与数据表及数据字段进行映射(如图4-2-3)。DataBindObject的这种映射机制,可以不必事先声明DataSet构架,当数据结构发生变动时,只需修改相应的业务实体。
public class Shipper:DataBindObject
{
[ColumnMapping("ShipperID", IsPrimaryKey=true, AutoIncrement=true)]
protected int pshipperID;
protected string pcompanyName;
public Shipper():base(){}
[ColumnMapping("CompanyName")]
public string CompanyName{
get{return pcompanyName;}
set{pcompanyName = value;}
}
public int ShipperID{get
【下篇】AutoComplete Real-time Query using[Jquery + Json + Webservices]