<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
  <channel>
    <title>yyliuliang</title>
    <description></description>
    <link>http://yyliuliang.javaeye.com</link>
    <language>UTF-8</language>
    <copyright>Copyright 2003-2008, JavaEye.com</copyright>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <generator>JavaEye - 做最棒的软件开发交流社区</generator>
      <item>
        <title>自己动手写个orm实现(1)</title>
        <author>yyliuliang</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://yyliuliang.javaeye.com">yyliuliang</a>&nbsp;
          链接：<a href="http://yyliuliang.javaeye.com/blog/196717" style="color:red;">http://yyliuliang.javaeye.com/blog/196717</a>&nbsp;
          发表时间: 2008年05月25日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p>最近公司项目开动了,出于各种考虑还是相对保守的采用 .NET 2.0 + 企业库 + SQLSERVER的大体框架.<br /><br />为了封装单个实体对象的CRUD操作,遂自己动手写了一个orm实现的工具类库. <br />-------------------------------------------------------------------------------------------------------<br />首先,我们声明一个实体接口&nbsp; IEntity</p>
<div style="padding-right: 5px; padding-left: 4px; font-size: 13px; padding-bottom: 4px; width: 98%; word-break: break-all; padding-top: 4px; background-color: #eeeeee; border: #cccccc 1px solid;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="" /><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">interface</span><span style="color: #000000;">&nbsp;IEntity&nbsp;:&nbsp;ICloneable<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_38_117_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_38_117_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_38_117_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_38_117_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" id="Codehighlighter1_38_117_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_38_117_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_38_117_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_38_117_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" /></span><span id="Codehighlighter1_38_117_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_38_117_Open_Text"><span style="color: #000000;">{<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_51_64_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_51_64_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_51_64_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_51_64_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_51_64_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_51_64_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_51_64_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_51_64_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;ID&nbsp;</span><span id="Codehighlighter1_51_64_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_51_64_Open_Text"><span style="color: #000000;">{&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">;&nbsp;</span><span style="color: #0000ff;">set</span><span style="color: #000000;">;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_86_94_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_86_94_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_86_94_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_86_94_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_86_94_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_86_94_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_86_94_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_86_94_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">bool</span><span style="color: #000000;">&nbsp;IsPersisted&nbsp;</span><span id="Codehighlighter1_86_94_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_86_94_Open_Text"><span style="color: #000000;">{&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_107_115_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_107_115_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_107_115_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_107_115_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_107_115_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_107_115_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_107_115_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_107_115_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;IEntity&nbsp;</span><span id="Codehighlighter1_107_115_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_107_115_Open_Text"><span style="color: #000000;">{&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" alt="" />}</span></span></div>
<p><br /><img src="http://222.173.176.83:88/orm/ientity.jpg" border="0" alt="" /><br /><br />根据图示, IEntity接口实现ICloneable接口, 并且有三个属性<br />ID : 对应每个实体类型所对应的数据库表的主键ID&nbsp; (有一个前言约定: 所有表的主键id均为自增标识)<br />IsPersisted : 指示对象是否已持久化(对应到数据库表的一条记录)<br />Raw : 返回对象实例的已持久化的原始状态<br /><br />有了实体,如何mapping到具体哪个数据表呢 ?<br />"约定优于配置", 因此我们直接使用Attribute来实现</p>
<div style="padding-right: 5px; padding-left: 4px; font-size: 13px; padding-bottom: 4px; width: 98%; word-break: break-all; padding-top: 4px; background-color: #eeeeee; border: #cccccc 1px solid;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="" /><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;EntityMappingAttribute&nbsp;:&nbsp;Attribute<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_52_354_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_52_354_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_52_354_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_52_354_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" id="Codehighlighter1_52_354_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_52_354_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_52_354_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_52_354_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_52_354_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_52_354_Open_Text"><span style="color: #000000;">{<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;_tableName;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;TableName<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_137_227_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_137_227_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_137_227_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_137_227_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_137_227_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_137_227_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_137_227_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_137_227_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_137_227_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_137_227_Open_Text"><span style="color: #000000;">{<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_155_177_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_155_177_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_155_177_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_155_177_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_155_177_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_155_177_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_155_177_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_155_177_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">&nbsp;</span><span id="Codehighlighter1_155_177_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_155_177_Open_Text"><span style="color: #000000;">{&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;_tableName;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_194_217_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_194_217_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_194_217_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_194_217_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_194_217_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_194_217_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_194_217_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_194_217_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">set</span><span style="color: #000000;">&nbsp;</span><span id="Codehighlighter1_194_217_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_194_217_Open_Text"><span style="color: #000000;">{&nbsp;_tableName&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;value;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;EntityMappingAttribute(</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;tableName)<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_301_347_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_301_347_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_301_347_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_301_347_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_301_347_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_301_347_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_301_347_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_301_347_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_301_347_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_301_347_Open_Text"><span style="color: #000000;">{<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TableName&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;tableName;<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span></div>
<p><br />同理,为了建立实体Property到数据表字段的对应关系我们新建PropertyMappingAttribute类</p>
<div style="padding-right: 5px; padding-left: 4px; font-size: 13px; padding-bottom: 4px; width: 98%; word-break: break-all; padding-top: 4px; background-color: #eeeeee; border: #cccccc 1px solid;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="" /><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;PropertyMappingAttribute&nbsp;:&nbsp;Attribute<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_54_390_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_54_390_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_54_390_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_54_390_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" id="Codehighlighter1_54_390_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_54_390_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_54_390_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_54_390_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_54_390_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_54_390_Open_Text"><span style="color: #000000;">{<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;_fieldName;<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;FieldName<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_132_222_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_132_222_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_132_222_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_132_222_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_132_222_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_132_222_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_132_222_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_132_222_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_132_222_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_132_222_Open_Text"><span style="color: #000000;">{<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_150_172_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_150_172_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_150_172_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_150_172_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_150_172_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_150_172_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_150_172_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_150_172_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">&nbsp;</span><span id="Codehighlighter1_150_172_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_150_172_Open_Text"><span style="color: #000000;">{&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;_fieldName;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_189_212_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_189_212_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_189_212_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_189_212_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_189_212_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_189_212_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_189_212_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_189_212_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">set</span><span style="color: #000000;">&nbsp;</span><span id="Codehighlighter1_189_212_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_189_212_Open_Text"><span style="color: #000000;">{&nbsp;_fieldName&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;value;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_266_269_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_266_269_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_266_269_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_266_269_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_266_269_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_266_269_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_266_269_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_266_269_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;PropertyMappingAttribute()&nbsp;</span><span id="Codehighlighter1_266_269_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_266_269_Open_Text"><span style="color: #000000;">{&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;PropertyMappingAttribute(</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;fieldName)<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_337_383_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_337_383_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_337_383_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_337_383_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_337_383_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_337_383_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_337_383_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_337_383_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_337_383_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_337_383_Open_Text"><span style="color: #000000;">{<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FieldName&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;fieldName;<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span></div>
<p><br />经过上述处理, 一个十分简单的面向对象实体(Object)到关系型数据表(Relationship)的映射(Mapping)就已经声明完毕了.<br /><br /><br />既然前面声明了IEntity接口, 理所当然的我们应该实现它</p>
<div style="padding-right: 5px; padding-left: 4px; font-size: 13px; padding-bottom: 4px; width: 98%; word-break: break-all; padding-top: 4px; background-color: #eeeeee; border: #cccccc 1px solid;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="" /><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">abstract</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;EntityBase&nbsp;:&nbsp;IEntity<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_47_1156_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_47_1156_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_47_1156_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_47_1156_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" id="Codehighlighter1_47_1156_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_47_1156_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_47_1156_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_47_1156_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_47_1156_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_47_1156_Open_Text"><span style="color: #000000;">{<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;IEntity&nbsp;_raw;<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;_id;<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">bool</span><span style="color: #000000;">&nbsp;_isPersisted&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">false</span><span style="color: #000000;">;<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_156_729_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_156_729_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_156_729_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_156_729_Open_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_156_729_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_156_729_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_156_729_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_156_729_Closed_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_156_729_Closed_Text" style="background-color: #ffffff; border: #808080 1px solid;">IEntity&nbsp;Members</span><span id="Codehighlighter1_156_729_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;">&nbsp;IEntity&nbsp;Members</span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_188_240_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_188_240_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_188_240_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_188_240_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_188_240_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_188_240_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_188_240_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_188_240_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_188_240_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;">/**/</span><span id="Codehighlighter1_188_240_Open_Text"><span style="color: #808080;">///</span><span style="color: #008000;">&nbsp;</span><span style="color: #808080;">&lt;summary&gt;</span><span style="color: #008000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080;">///</span><span style="color: #008000;">&nbsp;ID<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080;">///</span><span style="color: #008000;">&nbsp;</span><span style="color: #808080;">&lt;/summary&gt;</span></span><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">virtual</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;ID<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_278_354_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_278_354_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_278_354_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_278_354_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_278_354_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_278_354_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_278_354_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_278_354_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_278_354_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_278_354_Open_Text"><span style="color: #000000;">{<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_296_311_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_296_311_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_296_311_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_296_311_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_296_311_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_296_311_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_296_311_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_296_311_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">&nbsp;</span><span id="Codehighlighter1_296_311_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_296_311_Open_Text"><span style="color: #000000;">{&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;_id;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_328_344_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_328_344_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_328_344_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_328_344_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_328_344_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_328_344_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_328_344_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_328_344_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">set</span><span style="color: #000000;">&nbsp;</span><span id="Codehighlighter1_328_344_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_328_344_Open_Text"><span style="color: #000000;">{&nbsp;_id&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;value;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">virtual</span><span style="color: #000000;">&nbsp;IEntity&nbsp;Raw<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_400_488_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_400_488_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_400_488_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_400_488_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_400_488_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_400_488_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_400_488_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_400_488_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_400_488_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_400_488_Open_Text"><span style="color: #000000;">{<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_418_434_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_418_434_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_418_434_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_418_434_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_418_434_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_418_434_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_418_434_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_418_434_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">&nbsp;</span><span id="Codehighlighter1_418_434_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_418_434_Open_Text"><span style="color: #000000;">{&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;_raw;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_461_478_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_461_478_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_461_478_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_461_478_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_461_478_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_461_478_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_461_478_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_461_478_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">set</span><span style="color: #000000;">&nbsp;</span><span id="Codehighlighter1_461_478_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_461_478_Open_Text"><span style="color: #000000;">{&nbsp;_raw&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;value;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_499_558_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_499_558_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_499_558_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_499_558_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_499_558_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_499_558_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_499_558_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_499_558_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_499_558_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;">/**/</span><span id="Codehighlighter1_499_558_Open_Text"><span style="color: #808080;">///</span><span style="color: #008000;">&nbsp;</span><span style="color: #808080;">&lt;summary&gt;</span><span style="color: #008000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080;">///</span><span style="color: #008000;">&nbsp;该对象是否已持久化<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #808080;">///</span><span style="color: #008000;">&nbsp;</span><span style="color: #808080;">&lt;/summary&gt;</span></span><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">virtual</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">bool</span><span style="color: #000000;">&nbsp;IsPersisted<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_606_710_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_606_710_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_606_710_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_606_710_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_606_710_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_606_710_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_606_710_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_606_710_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_606_710_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_606_710_Open_Text"><span style="color: #000000;">{<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_624_648_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_624_648_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_624_648_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_624_648_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_624_648_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_624_648_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_624_648_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_624_648_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">&nbsp;</span><span id="Codehighlighter1_624_648_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_624_648_Open_Text"><span style="color: #000000;">{&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;_isPersisted;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_675_700_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_675_700_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_675_700_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_675_700_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_675_700_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_675_700_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_675_700_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_675_700_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">set</span><span style="color: #000000;">&nbsp;</span><span id="Codehighlighter1_675_700_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_675_700_Open_Text"><span style="color: #000000;">{&nbsp;_isPersisted&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;value;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_740_1150_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_740_1150_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_740_1150_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_740_1150_Open_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_740_1150_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_740_1150_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_740_1150_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_740_1150_Closed_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_740_1150_Closed_Text" style="background-color: #ffffff; border: #808080 1px solid;">ICloneable&nbsp;Members</span><span id="Codehighlighter1_740_1150_Open_Text" style="display: none;"><span style="color: #0000ff;">#region</span><span style="color: #000000;">&nbsp;ICloneable&nbsp;Members</span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">object</span><span style="color: #000000;">&nbsp;Clone()<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_806_1130_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_806_1130_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_806_1130_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_806_1130_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_806_1130_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_806_1130_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_806_1130_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_806_1130_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_806_1130_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_806_1130_Open_Text"><span style="color: #000000;">{<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Type&nbsp;type&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.GetType();<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">object</span><span style="color: #000000;">&nbsp;obj&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;Activator.CreateInstance(type);<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">foreach</span><span style="color: #000000;">&nbsp;(PropertyInfo&nbsp;p&nbsp;</span><span style="color: #0000ff;">in</span><span style="color: #000000;">&nbsp;type.GetProperties())<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_980_1095_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_980_1095_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_980_1095_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_980_1095_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_980_1095_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_980_1095_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_980_1095_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_980_1095_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_980_1095_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_980_1095_Open_Text"><span style="color: #000000;">{<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">object</span><span style="color: #000000;">&nbsp;value&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;p.GetValue(</span><span style="color: #0000ff;">this</span><span style="color: #000000;">,</span><span style="color: #0000ff;">null</span><span style="color: #000000;">);<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p.SetValue(obj,&nbsp;value,&nbsp;</span><span style="color: #0000ff;">null</span><span style="color: #000000;">);<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;obj;<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" /><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">#endregion</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span></div>
<p><br />由于IEntity 实现了ICloneable接口, 实际上肩负重任的抽象类EntityBase需要实现两个接口.<br />因为我们不能向外界暴露设置实体是否持久化和原始状态的信息的设置器, 我们把set修饰为protected.<br />而实现ICloneable比较简单,就是利用反射构造出一个当前实体对象实例的拷贝.<br /><br /><br />基础工作差不多了, 让我们我们新建一个User </p>
<div style="padding-right: 5px; padding-left: 4px; font-size: 13px; padding-bottom: 4px; width: 98%; word-break: break-all; padding-top: 4px; background-color: #eeeeee; border: #cccccc 1px solid;"><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="" /><span style="color: #000000;">[Serializable]<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="" />[EntityMapping(</span><span style="color: #800000;">"</span><span style="color: #800000;">t_user</span><span style="color: #800000;">"</span><span style="color: #000000;">)]<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" align="top" alt="" /></span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;User : EntityBase<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" id="Codehighlighter1_59_206_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_59_206_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_59_206_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_59_206_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" id="Codehighlighter1_59_206_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_59_206_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_59_206_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_59_206_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" /></span><span id="Codehighlighter1_59_206_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_59_206_Open_Text"><span style="color: #000000;">{<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;[PropertyMapping]<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_106_117_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_106_117_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_106_117_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_106_117_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_106_117_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_106_117_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_106_117_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_106_117_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;Name&nbsp;</span><span id="Codehighlighter1_106_117_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_106_117_Open_Text"><span style="color: #000000;">{&nbsp;</span><span style="color: #0000ff;">get</span><span style="color: #000000;">;</span><span style="color: #0000ff;">set</span><span style="color: #000000;">;}</span></span><span style="color: #000000;"><br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif" align="top" alt="" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">public</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">string</span><span style="color: #000000;">&nbsp;Hello&nbsp;<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_152_203_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_152_203_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_152_203_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_152_203_Closed_Text').style.display='inline';" align="top" alt="" /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" id="Codehighlighter1_152_203_Closed_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_152_203_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_152_203_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_152_203_Open_Text').style.display='inline';" align="top" alt="" style="display: none;" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_152_203_Closed_Text" style="display: none; background-color: #ffffff; border: #808080 1px solid;"><img src="http://www.cnblogs.com/Images/dot.gif" alt="" /></span><span id="Codehighlighter1_152_203_Open_Text"><span style="color: #000000;">{&nbsp;<br /><img src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" id="Codehighlighter1_167_197_Open_Image" onclick="this.style.display='none'; document.getElementById('Codehighlighter1_167_197_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_167_197_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_167_197_Closed_Text').style.display='inline';" align="top" alt="" /><img id="Codehighlighter1_167_197_Closed_Image" style="display: none;" onclick</span></span></span></div>
          <br/>
          <span style="color:red;">
            <a href="http://yyliuliang.javaeye.com/blog/196717#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Sun, 25 May 2008 16:05:39 +0800</pubDate>
        <link>http://yyliuliang.javaeye.com/blog/196717</link>
        <guid>http://yyliuliang.javaeye.com/blog/196717</guid>
      </item>
      <item>
        <title>深入ASP.NET AJAX : 对象继承体系</title>
        <author>yyliuliang</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://yyliuliang.javaeye.com">yyliuliang</a>&nbsp;
          链接：<a href="http://yyliuliang.javaeye.com/blog/187384" style="color:red;">http://yyliuliang.javaeye.com/blog/187384</a>&nbsp;
          发表时间: 2008年04月28日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          <p>原文： <a href="http://weblogs.asp.net/stephenwalther/archive/2008/03/05/asp-net-ajax-in-depth-object-inheritance.aspx" target="_blank">ASP.NET AJAX In-Depth: Object Inheritance </a>  作者：<a href="http://weblogs.asp.net/stephenwalther/" target="_blank">Stephen Walther</a><br /><br />来源：http://www.cnblogs.com/dflying/archive/2008/03/16/1108763.html   <br /><br />---------------------------------------------------------------------   <br /><br />本文的目的很简单 ： 就是在享用ASP.NET AJAX框架对象继承体系带来的好处同时，我想弄清在这鼓里究竟埋了什么东东。 让我们从这段简单的代码开始 。。。<br /><br />// 代码1 &ndash; AjaxInheritance.aspx<br />   1:  &lt;script type="text/javascript"&gt;<br />   2: <br />     3:  function BaseControl()<br />   4:  {<br />   5:      this._propA = "BaseControl propA";<br />   6:      this._propB = "BaseControl propB";<br />   7:  }<br />   8: <br />     9:  BaseControl.prototype =<br />  10:  {<br />  11:      get_propA : function() {return this._propA;},<br />  12:      set_propA : function(value) {this._propA = value; },<br />  13: <br />    14:      get_propB : function() {return this._propB; },<br />  15:      set_propB : function(value) {this._propB = value; }<br />  16:  }<br />  17: <br />    18:  BaseControl.registerClass("BaseControl");<br />  19: <br />    20: <br />    21:  function TreeViewControl()<br />  22:  {<br />  23:      TreeViewControl.initializeBase(this);<br />  24:      this._propA = "TreeViewControl propA";<br />  25:  }<br />  26: <br />    27:  TreeViewControl.prototype =<br />  28:  {<br />  29:      get_propA : function() {return this._propA;},<br />  30:      set_propA : function(value) {this._propA = value; }<br />  31:  }<br />  32: <br />    33:  TreeViewControl.registerClass("TreeViewControl", BaseControl);<br />  34: <br />    35:  var treeView1 = new TreeViewControl();<br />  36: <br />    37:  alert( treeView1.get_propA() ); // displays "TreeViewControl propA"<br />  38:  alert( treeView1.get_propB() ); // displays "BaseControl propB"<br />  39: <br />    40: <br />       41:  &lt;/script&gt;<br /><br /><br /><br />上面的这段代码定义了BaseControl和TreeViewControl两个对象。他们兴许代表了web页里展示的两个对象。TreeViewControl继承自BaseControl。   <br /><br />首先，BaseControl定义了一个名为BaseControl的构造函数。构造函数初始化了名为_propA和_propB的两个变量。接下来，用以提供向外界提供访问私有变量_propA,_propB的途径，set,get方法被作为公开属性添加到BaseControl的原型中。最后，利用ASP.NET AJAX客户端框架提供的registerClass方法，将BaseControl对象注册为一个类。在稍后我们将讨论调用registerClass方法其中的细节。   <br /><br />其后，定义一个新对象TreeViewControl。大家注意到，TreeViewControl的构造函数里调用了一个名为initializeBase的ASP.NET AJAX方法。后文同样会就这个方法进行讨论。   <br /><br />类似BaseControl,TreeViewControl通过调用registerClass注册为一个类，但是TreeViewControl确是作为BaseControl的子类.传递到registerClass方法中的第二个参数能够让你指定父类   <br /><br />最后,创建了名为treeView1的TreeViewControl实例,get_propA()  和get_propB() 方法被调用并显示出测试结果   <br /><br /><strong>理解registerClass()方法</strong><br /><br />为了探究上文中的细节,我们第一个要了解的ASP.NET AJAX方法就是registerClass.它接收以下三个参数   <br /><br />&middot; typeName &ndash;  注册的类名   <br /><br />&middot; baseType &ndash;  被注册类的父类 [ 可选 ]    <br /><br />&middot; interfaceTypes &ndash;  被注册类需要实现的接口列表 [ 可选 ]   <br /><br />通过参阅<a href="http://msdn2.microsoft.com/en-us/asp.net/bb944808.aspx" target="_blank">Microsoft Ajax框架</a>中的MicrosoftAjax.debug.js文件你能够将registerClass其中细节一览无余.  <br /><br />下面截取的代码片段向我们展示了registerClass方法的相关信息<br /><br />//&nbsp;代码2 &ndash; MicrosoftAjax.debug.js registerClass() method<br />   1:  Type.prototype.registerClass = function(typeName, baseType, interfaceTypes) {<br />   2: <br />     3:      var parsedName;<br />   4:      try {<br />   5:          parsedName = eval(typeName);<br />   6:      }<br />   7:      catch(e) {<br />   8:          throw Error.argument('typeName', Sys.Res.argumentTypeName);<br />   9:      }<br />  10:      if (parsedName !== this) throw Error.argument('typeName', Sys.Res.badTypeName);<br />  11:      if (Sys.__registeredTypes[typeName]) throw Error.invalidOperation(String.format(Sys.Res.typeRegisteredTwice, typeName));<br />  12:      if (baseType &amp;&amp; !baseType.__class) throw Error.argument('baseType', Sys.Res.baseNotAClass);<br />  13:      this.prototype.constructor = this;<br />  14:      this.__typeName = typeName;<br />  15:      this.__class = true;<br />  16:      if (baseType) {<br />  17:          this.__baseType = baseType;<br />  18:          this.__basePrototypePending = true;<br />  19:      }<br />  20:      Sys.__upperCaseTypes[typeName.toUpperCase()] = this;<br />  21: <br />    22:      Sys.__registeredTypes[typeName] = true;<br />  23:      return this;<br />  24:  }<br /><br /><br /> 在上面的代码中你首先需要注意的就是registerClass是一个名为"Type"对象的方法(它定义在Type原型中).那Type是一个什么东东呢?原来它只是JavaScript中Function的别名而已. Microsoft AJAX框架用以这段代码创建别名:  <br /><br />window.Type = Function;  <br /><br />这样一来,我们就能够在任何function中调用registerClass方法了,通常来说,你应该在构造函数中调用它.  <br /><br />从下面的代码开始,让我们步步深入,探究registerClass的其中种种.<br /><br /> 1:  var parsedName;<br />   2:      try {<br />   3:          parsedName = eval(typeName);<br />   4:      }<br />   5:      catch(e) {<br />   6:          throw Error.argument('typeName', Sys.Res.argumentTypeName);<br />   7:      }<br />   8:      if (parsedName !== this) throw Error.argument('typeName', Sys.Res.badTypeNam<br /><br />当你调用registerClass方法时,第一个字符串型参数应该是当前调用者的对象名. 用以下你能够调用registerClass方法注册名为TreeViewControl的类:<br /><br />TreeViewControl.registerClass(&ldquo;TreeViewControl&rdquo;);<br /><br />这样捣鼓看起来是不是显得有些多余?为什么需要引用TreeViewControl两次呢:一次是作为对象,一次是字符串形式. 我认为这样做的目的是为了获取字符串形式的函数名,用以校验赋予的string值是否等价于真实的参数. 有一种方法,我们可以避免类似的冗余:将要注册的函数内调用toString()方法来检验函数名 :<br /><br />this.toString().match(/( \w+)/)[0] <br /><br />我推测,Microsft AJAX没有这样做的原因可能是出于性能考虑.  <br /><br />接下来的代码<br /><br /> 1: if (Sys.__registeredTypes[typeName]) throw Error.invalidOperation(String.format(Sys.Res.typeRegisteredTwice, typeName));<br />   2: if (baseType &amp;&amp; !baseType.__class) throw Error.argument('baseType', Sys.Res.baseN<br /><br /><br />第一行代码能够防止同一类名被多次注册,第二行则验证传入的父类参数是否实际存在.<br /><br />this.prototype.constructor = this;<br /><br />这行代码很好很有趣.它fix了Javascript中constuctor属性的bug,如下<br /><br />//&nbsp;代码3 &ndash; BadConstructor.js<br />   1:      &lt;script type="text/javascript"&gt;<br />   2: <br />     3:      // Bad Constructor<br />   4:      function A() {}<br />   5:      function B() {}<br />   6: <br />        7:      B.prototype = new A();<br />   8: <br />        9:      var x = new B();<br />  10:      alert( x.constructor ); // Returns A<br />  11: <br />    12: <br />    13:      // Good Constructor<br />  14:      function A() {}<br />  15:      function B() {}<br />  16: <br />       17:      B.prototype = new A();<br />  18:      B.prototype.constructor = B; // fix constructor<br />  19: <br />       20:      var x = new B();<br />  21:      alert( x.constructor ); // Returns B<br />  22: <br />    23: <br />       24:      &lt;/script&gt;<br /><br /><br /><br />如上,构造函数应该返回当前构造对象的函数名.不幸的是,因为prototype chain的存在,constructor属性返回了错误的构造函数,它返回的是构造prototype chain顶部对象的函数名称.当[代码3]中的x.constructor第一次被调用,其返回了一个错误值.经过后面的处理,x.constructor才返回了正确值.<br /><br />1:      this.__typeName = typeName;<br />   2:      this.__class = true;<br />   3:      if (baseType) {<br />   4:          this.__baseType = baseType;<br />   5:          this.__basePrototypePending = true;<br />   6:      }<br />   7:      Sys.__upperCaseTypes[typeName.toUpperCase()] = this;<br />   8: <br />     9:      Sys.__registeredTypes[typeName] = true;<br /><br /><br />在这里,为当前构造函数添加了一些属性:__typeName, __class,__baseType, 和 __basePrototypePending.这些属性会被一些方法例如initializeBase()进行反射处理,详情稍后讨论.  <br /><br />最后,构造函数名被Sys命名空间中的__upperCaseTypes 和__registeredTypes数组所保存.__upperCaseTypes被Type.parse方法用递对象名来生成对象实例.__registeredTypes数组则被registerClass方法用来确认类没有被重复注册.你需要注意到的是registerClass方法并没有修改prototype属性,直到initializeBase的出现....  <br /><br /><strong>理解 initializeBase 方法</strong><br /><br />你可以在派生类的构造函数中调用initializeBase方法.例如在[代码1]中,我们在TreeViewControl的构造函数中调用initializeBase方法.如果你不这么做,派生类将不能从其父类中继承到任何东东.<br /><br />1:  Type.prototype.initializeBase = function(instance, baseArguments) {<br />   2:      if (!this.isInstanceOfType(instance)) throw Error.argumentType('instance', Object.getType(instance), this);<br />   3:      this.resolveInheritance();<br />   4:      if (this.__baseType) {<br />   5:          if (!baseArguments) {<br />   6:              this.__baseType.apply(instance);<br />   7:          }<br />   8:          else {<br />   9:              this.__baseType.apply(instance, baseArguments);<br />  10:          }<br />  11:      }<br />  12:      return instance;<br />  13:  }<br /><br />和registerClass方法一样,initializeBase也被创建为Function对象的方法(Function对象在Microsoft Ajax中的别名为Type). 这样你就能在任何function中调用initializeBase.这样做的意义在于你能够在当前function或当前function的父类的构造函数中调用initializeBase  <br /><br />if (!this.isInstanceOfType(instance))  <br /><br /> throw Error.argumentType('instance', Object.getType)  <br /><br />这段代码验证了传递到initializeBase方法中的实例参数是否和当前调用者(或是当前调用者的父类)相同.如下<br /><br />//&nbsp;代码5 &ndash; BadInitializeBase.js <br />   1:  &lt;script type="text/javascript"&gt;<br />   2: <br />     3:  function A() {}<br />   4:  A.registerClass("A");<br />   5:  function B() {}<br />   6:  B.registerClass("B");<br />   7:  function C()<br />   8:  {<br />   9:      A.initializeBase(this); // throws exception<br />  10:      B.initializeBase(this); // ok<br />  11:      C.initializeBase(this); // ok<br />  12:  } <br />     13:  C.registerClass("C", B);<br />  14: <br />    15:  var x = new C();<br />  16: <br />    17: <br />    18:  &lt;/script&gt;<br /><br /><br /><br />注意,下面这段非常重要!  <br /><br />this.resolveInheritance();  <br /><br />通过调用resolveInheritance方法修改了当前对象的原型<br /><br />//&nbsp;代码&nbsp;6 &ndash; MicrosoftAjax.debug.js resolveInheritance()<br />   1:  Type.prototype.resolveInheritance = function() {<br />   2:      if (this.__basePrototypePending) {<br />   3:          var baseType = this.__baseType;<br />   4:          baseType.resolveInheritance();<br />   5:          for (var memberName in baseType.prototype) {<br />   6:              var memberValue = baseType.prototype[memberName];<br />   7:              if (!this.prototype[memberName]) {<br />   8:                  this.prototype[memberName] = memberValue;<br />   9:              }<br />  10:          }<br />  11:          delete this.__basePrototypePending;<br />  12:      }<br />  13:  }<br /><br /><br />resolveInheritance方法检验构造函数的__basePrototypePending属性是否为真.当你注册一个新的类时,registerClass方法会将__basePrototypePending设置为true.  <br />接下来,resolveInheritance方法会被继承链中每个对象的构造函数重复调用.自顶向下,继承链中每个对象的属性被依次拷贝.<br /><br />//&nbsp;代码&nbsp;7 &ndash; AjaxInheritance.js<br /><br />   1: &lt;script type="text/javascript"&gt;<br />   2: <br />     3:  function BaseControl()<br />   4:  {<br />   5:      this._propA = "BaseControl propA";<br />   6:      this._propB = "BaseControl propB";<br />   7:  }<br />   8: <br />     9:  BaseControl.prototype =<br />  10:  {<br />  11:      get_propA : function() {return this._propA;},<br />  12:      set_propA : function(value) {this._propA = value; },<br />  13: <br />    14:      get_propB : function() {return this._propB; },<br />  15:      set_propB : function(value) {this._propB = value; }<br />  16:  }<br />  17: <br />    18:  BaseControl.registerClass("BaseControl");<br />  19: <br />    20: <br />    21:  function TreeViewControl()<br />  22:  {<br />  23:      TreeViewControl.initializeBase(this);<br />  24:      this._propA = "TreeViewControl propA";<br />  25:  }<br />  26: <br />    27:  TreeViewControl.prototype =<br />  28:  {<br />  29:      get_propA : function() {return this._propA;},<br />  30:      set_propA : function(value) {this._propA = value; }<br />  31:  }<br />  32: <br />    33:  TreeViewControl.registerClass("TreeViewControl", BaseControl);<br />  34: <br />    35:  var treeView1 = new TreeViewControl();<br />  36: <br />    37:  alert( treeView1.get_propA() ); // displays "TreeViewControl propA"<br />  38:  alert( treeView1.get_propB() ); // displays "BaseControl propB"<br />  39: <br />    40: <br />       41:  &lt;/script&gt;<br /><br /><br />在TreeViewControl的构造函数中调用initializeBase方法时,resolveInheritance方法亦被调用.resolveInheritance方法检索TreeViewControl的父类对象.在这里,TreeViewControl的父类是BaseControl,BaseControl的每一个属性都被拷贝到TreeViewControl的constructor原型中.  <br /><br />为什么当前对象原型的属性只能从父类中拷贝呢? 换言之,why are theprototypes flattened? 出于性能原因? 如果原型链中的所有属性都拷贝到最底层中,那么就没有必要解析原型链中的全部属性.  <br /><br />在FireFox中,下面表达式返回true<br /><br />alert( treeView1.__proto__.hasOwnProperty("get_propB") ); // displays true<br /><br />__proto__ property是FireFox的专有属性,表示了当前对象的原型.hasOwnProperty方法只有当一个属性为对象的本身属性而不是从原型链中继承时才返回true.上述代码显示了该原型已被flattened并且读取该属性已脱离原型链.  <br /><br />initializeBase方法执行的最后一段代码如下<br /><br /> 1:      if (this.__baseType) {<br />   2:          if (!baseArguments) {<br />   3:              this.__baseType.apply(instance);<br />   4:          }<br />   5:          else {<br />   6:              this.__baseType.apply(instance, baseArguments);<br />   7:          }<br />   8:      }<br /><br />这段代码检验了当前的构造函数是否通过registerClass方法指定父类.如果存在父类,apply方法将父类已创建的对象传递给当前的构造函数.apply方法展示了treeView1对象是如何获得_propA和_propB对象的.</p>
<p>&nbsp;</p>
          <br/>
          <span style="color:red;">
            <a href="http://yyliuliang.javaeye.com/blog/187384#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Mon, 28 Apr 2008 01:21:06 +0800</pubDate>
        <link>http://yyliuliang.javaeye.com/blog/187384</link>
        <guid>http://yyliuliang.javaeye.com/blog/187384</guid>
      </item>
      <item>
        <title>读过的好书列表[技术类]</title>
        <author>yyliuliang</author>
        <description>
          <![CDATA[
          <br/>
          作者: <a href="http://yyliuliang.javaeye.com">yyliuliang</a>&nbsp;
          链接：<a href="http://yyliuliang.javaeye.com/blog/156853" style="color:red;">http://yyliuliang.javaeye.com/blog/156853</a>&nbsp;
          发表时间: 2008年01月17日
          <br/><br/>
          声明：本文系JavaEye网站发布的原创博客文章，未经作者书面许可，严禁任何网站转载本文，否则必将追究法律责任！
          <br/><br/>
          >>.NET框架程序设计  [框架设计]  ISBN9787302140160 <br />首屈一指的比较全面介绍.NET框架的书籍.  从CLR,到C#语法以及框架底层实现细节都做了概况介绍.能够引导对.NET底层了解不透彻的读者发现,钻研.NET的方方面面,而不仅仅是&lt;&lt;XXX实用技术>>等粗制滥造的垃圾书讲一些拖拉控件的快速开发.<br />另: 推荐阅读C#2.0版讲述的&lt;&lt;框架设计>>  <br /><br />>>SQLSERVER2000高级DBA指南   ISBN 9787302072843 <br />虽然讲述的SQLSERVER的版本较低,一些很有用的新概念,技术没有覆盖.但是其中讲述的内容,对每一个想深入了解SQLSERVER的读者有很大帮助,尤其推荐其中的性能调优章节.<br /><br />>>JAVASCRIPT权威指南  ISBN 9787111216322 <br />每一位WEB程序员应该人手一本的工具书.<br /><br />>>深入浅出HIBERNATE     ISBN 9787121006708 <br />借助HIBERNATE把ORM讲述的浅显易懂,即使是没有ORM使用经验的读者也能从中获得愉快的阅读体验.<br /><br />>>道不远人 - 深入解析ASP.NET 控件开发   ISBN9787121049088<br /><br />由博客园的MVP - THIN撰写的专门关于ASP.NET控件开发知识的书籍. 一直以来,由于.NET知识的易学难精,进入门槛较低,导致产生了很多只会拖拉控件的程序员.但是控件具体的生死存亡,就全然不知了.作者由一个个实际生产中可以用到的技术点展开, 向读者深入浅出的剖析了.NET控件的设计结构,生命周期,以及如何扩展, 以实践的方式阐述知识.<br /><br /><br /><br />TO BECONTINUED
          <br/>
          <span style="color:red;">
            <a href="http://yyliuliang.javaeye.com/blog/156853#comments" style="color:red;">本文的讨论也很精彩，浏览讨论>></a>
          </span>
          <br/><br/><br/>
          <span style="color:#E28822;">JavaEye推荐</span>
          <br/>
          <ul class='adverts'><li><a href='/adverts/42' target='_blank'><span style="color:red;font-weight:bold;">搜狐网站诚聘Java、PHP和C++工程师</span></a></li><li><a href='/adverts/41' target='_blank'><span style="color:red;font-weight:bold;">北京: 千橡集团暨校内网诚聘软件研发工程师</span></a></li></ul>
          <br/><br/><br/>
          ]]>
        </description>
        <pubDate>Thu, 17 Jan 2008 11:48:44 +0800</pubDate>
        <link>http://yyliuliang.javaeye.com/blog/156853</link>
        <guid>http://yyliuliang.javaeye.com/blog/156853</guid>
      </item>
  </channel>
</rss>