一般来说,对系统的分层,一般都需要下面几个层:实体层(Entity)、数据访问层(DAL)、业务逻辑层(BLL)、界面层(UI);而数据访问层,一般也会加入一个接口层(IDAL)。
在其中的实体层,一般是根据数据库进行映射外加入注释等,技术含量不大,在此一笔带过;数据库访问层和业务逻辑层,是关键之所在,因为这里好的设计,会利用很多基类的操作,减少很多代码和重复劳动;界面层,不管是WebForm还是WinForm,都是尽可能少的逻辑代码或者SQL语句在其中,好的项目可能会利用一些优秀的控件进去,提高体验,减少代码。另外,由于一些创建操作费时费资源,一般还需要把可重复利用的资源缓存起来,提高性能。
先给大家预览下项目的框架,再一层层分析讨论:
EnterpriseLib.jpg

1、 实体层(定义一个空的基类,其他实体类继承之,主要是为了利用泛型操作,用途下面细说)

None.gif    public class BaseEntity
ExpandedBlockStart.gifContractedBlock.gif    dot.gif{    
ExpandedBlockEnd.gif    }

 

None.gif    public class EquipmentInfo : BaseEntity
ExpandedBlockStart.gifContractedBlock.gif    dot.gif{    
ContractedSubBlock.gifExpandedSubBlockStart.gif        Field Members#region Field Members
InBlock.gif
InBlock.gif        private int m_ID = 0; //ID          
InBlock.gif        private string m_PartID = ""; //备件编号   InBlock.gif        
InBlock.gif        //dot.gifdot.gifdot.gif
InBlock.gif
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        Property Members#region Property Members
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// ID
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        public virtual int ID
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            get
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                return this.m_ID;
ExpandedSubBlockEnd.gif            }
InBlock.gif            set
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                this.m_ID = value;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 备件编号
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        public virtual string PartID
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            get
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                return this.m_PartID;
ExpandedSubBlockEnd.gif            }
InBlock.gif            set
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                this.m_PartID = value;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        //dot.gifdot.gifdot.gif
InBlock.gif
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif
ExpandedBlockEnd.gif    }

2、 数据库访问层,数据访问层的关键是数据访问基类的设计,基类实现大多数数据库的日常操作,如下:

ExpandedBlockStart.gifContractedBlock.gif    /**//// <summary>
InBlock.gif    /// 数据访问层的基类
ExpandedBlockEnd.gif    /// </summary>
None.gif    public abstract class BaseDAL<T> : IBaseDAL<T> where T : BaseEntity, new()
ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
ExpandedBlockEnd.gif}

BaseEntity就是实体类的基类,IBaseDAL是定义的数据访问基类接口,包含各种常用的操作定义;因此BaseDAL就是要对各种操作的进行实现,实现接口越多,将来继承类的重用程度就越高。
以上通过泛型<T> ,我们就可以知道实例化那个具体访问类的信息了,可以实现强类型的函数定义。

ExpandedBlockStart.gifContractedBlock.gif    /**//// <summary>
InBlock.gif    /// 一些基本的,作为辅助函数的接口
ExpandedBlockEnd.gif    /// </summary>
None.gif    public interface IBaseDAL<T> where T : BaseEntity
ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 查询数据库,检查是否存在指定键值的对象
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="recordTable">Hashtable:键[key]为字段名;值[value]为字段对应的值</param>
ExpandedSubBlockEnd.gif        /// <returns>存在则返回<c>true</c>,否则为<c>false</c>。</returns>
InBlock.gif        bool IsExistKey(Hashtable recordTable);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 查询数据库,检查是否存在指定键值的对象
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="fieldName">指定的属性名</param>
InBlock.gif        /// <param name="key">指定的值</param>
ExpandedSubBlockEnd.gif        /// <returns>存在则返回<c>true</c>,否则为<c>false</c>。</returns>
InBlock.gif        bool IsExistKey(string fieldName, object key);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 获取数据库中该对象的最大ID值
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <returns>最大ID值</returns>
InBlock.gif        int GetMaxID();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 根据指定对象的ID,从数据库中删除指定对象
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="key">指定对象的ID</param>
ExpandedSubBlockEnd.gif        /// <returns>执行成功返回<c>true</c>,否则为<c>false</c>。</returns>
InBlock.gif        bool DeleteByKey(string key);
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 根据条件,从数据库中删除指定对象
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="condition">删除记录的条件语句</param>
ExpandedSubBlockEnd.gif        /// <returns>执行成功返回<c>true</c>,否则为<c>false</c>。</returns>
InBlock.gif        bool DeleteByCondition(string condition);
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 插入指定对象到数据库中
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="obj">指定的对象</param>
ExpandedSubBlockEnd.gif        /// <returns>执行成功返回True</returns>
InBlock.gif        bool Insert(T obj);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 更新对象属性到数据库中
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="obj">指定的对象</param>
ExpandedSubBlockEnd.gif        /// <returns>执行成功返回<c>true</c>,否则为<c>false</c>。</returns>
InBlock.gif        bool Update(T obj, string primaryKeyValue);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 查询数据库,检查是否存在指定ID的对象(用于整型主键)
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="key">对象的ID值</param>
ExpandedSubBlockEnd.gif        /// <returns>存在则返回指定的对象,否则返回Null</returns>
InBlock.gif        T FindByID(int key);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 查询数据库,检查是否存在指定ID的对象(用于字符型主键)
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="key">对象的ID值</param>
ExpandedSubBlockEnd.gif        /// <returns>存在则返回指定的对象,否则返回Null</returns>
InBlock.gif        T FindByID(string key);
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        返回集合的接口#region 返回集合的接口
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 根据ID字符串(逗号分隔)获取对象列表
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="idString">ID字符串(逗号分隔)</param>
ExpandedSubBlockEnd.gif        /// <returns>符合条件的对象列表</returns>
InBlock.gif        List<T> FindByIDs(string idString);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 根据条件查询数据库,并返回对象集合
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="condition">查询的条件</param>
ExpandedSubBlockEnd.gif        /// <returns>指定对象的集合</returns>
InBlock.gif        List<T> Find(string condition);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="condition">查询的条件</param>
InBlock.gif        /// <param name="info">分页实体</param>
ExpandedSubBlockEnd.gif        /// <returns>指定对象的集合</returns>
InBlock.gif        List<T> Find(string condition, PagerInfo info);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 返回数据库所有的对象集合
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <returns>指定对象的集合</returns>
InBlock.gif        List<T> GetAll();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 返回数据库所有的对象集合(用于分页数据显示)
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="info">分页实体信息</param>
ExpandedSubBlockEnd.gif        /// <returns>指定对象的集合</returns>                
InBlock.gif        List<T> GetAll(PagerInfo info);
InBlock.gif
InBlock.gif        DataSet GetAllToDataSet(PagerInfo info);
InBlock.gif
ExpandedSubBlockEnd.gif        #endregion
ExpandedBlockEnd.gif    }

细看上面代码,会发现由一个PagerInfo 的类,这个类是用来做分页参数传递作用的,根据这个参数,你可以知道具体返回那些关心的记录信息,这些记录又转换为强类型的List<T>集合。
再看看数据库访问基类的具体实现代码吧:
ExpandedBlockStart.gifContractedBlock.gif    /**//// <summary>
InBlock.gif    /// 数据访问层的基类
ExpandedBlockEnd.gif    /// </summary>
None.gif    public abstract class BaseDAL<T> : IBaseDAL<T> where T : BaseEntity, new()
ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        构造函数#region 构造函数
InBlock.gif
InBlock.gif        protected string tableName;//需要初始化的对象表名
InBlock.gif        protected string primaryKey;//数据库的主键字段名
InBlock.gif        protected string sortField = "LastUpdated";//排序字段
InBlock.gif        private bool isDescending = true;//
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 排序字段
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        public string SortField
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            get 
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                return sortField; 
ExpandedSubBlockEnd.gif            }
InBlock.gif            set 
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                sortField = value; 
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 是否为降序
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        public bool IsDescending
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gif{ return isDescending; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ isDescending = value; }
ExpandedSubBlockEnd.gif        }       
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 数据库访问对象的表名
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        public string TableName
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            get
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                return tableName;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 数据库访问对象的外键约束
ExpandedSubBlockEnd.gif        /// </summary>
InBlock.gif        public string PrimaryKey
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            get
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                return primaryKey;
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif        
InBlock.gif        public BaseDAL()
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{}
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 指定表名以及主键,对基类进构造
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="tableName">表名</param>
ExpandedSubBlockEnd.gif        /// <param name="primaryKey">表主键</param>
InBlock.gif        public BaseDAL(string tableName, string primaryKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            this.tableName = tableName;
InBlock.gif            this.primaryKey = primaryKey;
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        通用操作方法#region 通用操作方法
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 添加记录
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="recordField">Hashtable:键[key]为字段名;值[value]为字段对应的值</param>
ExpandedSubBlockEnd.gif        /// <param name="trans">事务对象,如果使用事务,传入事务对象,否则为Null不使用事务</param>
InBlock.gif        public bool Insert(Hashtable recordField, DbTransaction trans)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            return this.Insert(recordField, tableName, trans);
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 添加记录
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="recordField">Hashtable:键[key]为字段名;值[value]为字段对应的值</param>
InBlock.gif        /// <param name="targetTable">需要操作的目标表名称</param>
ExpandedSubBlockEnd.gif        /// <param name="trans">事务对象,如果使用事务,传入事务对象,否则为Null不使用事务</param>
InBlock.gif        public bool Insert(Hashtable recordField, string targetTable, DbTransaction trans)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            bool result = false;
InBlock.gif            string fields = ""; // 字段名
InBlock.gif            string vals = ""; // 字段值
InBlock.gif            if ( recordField == null || recordField.Count < 1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                return result;
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            SqlParameter[] param = new SqlParameter[recordField.Count];
InBlock.gif            IEnumerator eKeys = recordField.Keys.GetEnumerator();
InBlock.gif
InBlock.gif            int i = 0;
InBlock.gif            while ( eKeys.MoveNext() )
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                string field = eKeys.Current.ToString();
InBlock.gif                fields += field + ",";
InBlock.gif                vals += string.Format("@{0},", field);
InBlock.gif                object val = recordField[eKeys.Current.ToString()];
InBlock.gif                param[i] = new SqlParameter("@" + field, val);
InBlock.gif
InBlock.gif                i++;
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            fields = fields.Trim(',');//除去前后的逗号
InBlock.gif            vals = vals.Trim(',');//除去前后的逗号
InBlock.gif            string sql = string.Format("INSERT INTO {0} ({1}) VALUES ({2})", targetTable, fields, vals);
InBlock.gif
InBlock.gif            Database db = DatabaseFactory.CreateDatabase();
InBlock.gif            DbCommand command = db.GetSqlStringCommand(sql);
InBlock.gif            command.Parameters.AddRange(param);
InBlock.gif
InBlock.gif            if ( trans != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                result = db.ExecuteNonQuery(command, trans) > 0;
ExpandedSubBlockEnd.gif            }
InBlock.gif            else
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                result = db.ExecuteNonQuery(command) > 0;
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            return result;
ExpandedSubBlockEnd.gif        }
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 更新某个表一条记录(只适用于用单键,用int类型作键值的表)
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="id">ID号</param>
InBlock.gif        /// <param name="recordField">Hashtable:键[key]为字段名;值[value]为字段对应的值</param>
ExpandedSubBlockEnd.gif        /// <param name="trans">事务对象,如果使用事务,传入事务对象,否则为Null不使用事务</param>
InBlock.gif        public bool Update(int id, Hashtable recordField, DbTransaction trans)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            return this.Update(id, recordField, tableName, trans);
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 更新某个表一条记录(只适用于用单键,用string类型作键值的表)
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="id">ID号</param>
InBlock.gif        /// <param name="recordField">Hashtable:键[key]为字段名;值[value]为字段对应的值</param>
ExpandedSubBlockEnd.gif        /// <param name="trans">事务对象,如果使用事务,传入事务对象,否则为Null不使用事务</param>
InBlock.gif        public bool Update(string id, Hashtable recordField, DbTransaction trans)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            return this.Update(id, recordField, tableName, trans);
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 更新某个表一条记录(只适用于用单键,用int类型作键值的表)
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="id">ID号</param>
InBlock.gif        /// <param name="recordField">Hashtable:键[key]为字段名;值[value]为字段对应的值</param>
InBlock.gif        /// <param name="targetTable">需要操作的目标表名称</param>
ExpandedSubBlockEnd.gif        /// <param name="trans">事务对象,如果使用事务,传入事务对象,否则为Null不使用事务</param>
InBlock.gif        public bool Update(int id, Hashtable recordField, string targetTable, DbTransaction trans)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            return Update(id, recordField, targetTable, trans);
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 更新某个表一条记录(只适用于用单键,用string类型作键值的表)
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="id">ID号</param>
InBlock.gif        /// <param name="recordField">Hashtable:键[key]为字段名;值[value]为字段对应的值</param>
InBlock.gif        /// <param name="targetTable">需要操作的目标表名称</param>
ExpandedSubBlockEnd.gif        /// <param name="trans">事务对象,如果使用事务,传入事务对象,否则为Null不使用事务</param>
InBlock.gif        public bool Update(string id, Hashtable recordField, string targetTable, DbTransaction trans)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            string field = ""; // 字段名
InBlock.gif            object val = null; // 值
InBlock.gif            string setValue = ""; // 更新Set () 中的语句
InBlock.gif
InBlock.gif            if ( recordField == null || recordField.Count < 1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                return false;
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            SqlParameter[] param = new SqlParameter[recordField.Count];
InBlock.gif            int i = 0;
InBlock.gif
InBlock.gif            IEnumerator eKeys = recordField.Keys.GetEnumerator();
InBlock.gif            while ( eKeys.MoveNext() )
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                field = eKeys.Current.ToString();
InBlock.gif                val = recordField[eKeys.Current.ToString()];
InBlock.gif                setValue += string.Format("{0} = @{0},", field);
InBlock.gif                param[i] = new SqlParameter(string.Format("@{0}", field), val);
InBlock.gif
InBlock.gif                i++;
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            string sql = string.Format("UPDATE {0} SET {1} WHERE {2} = '{3}' ", targetTable, setValue.Substring(0, setValue.Length - 1), primaryKey, id);
InBlock.gif            Database db = DatabaseFactory.CreateDatabase();
InBlock.gif            DbCommand command = db.GetSqlStringCommand(sql);
InBlock.gif            command.Parameters.AddRange(param);
InBlock.gif
InBlock.gif            bool result = false;
InBlock.gif            if (trans != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                result = db.ExecuteNonQuery(command, trans) > 0;
ExpandedSubBlockEnd.gif            }
InBlock.gif            else
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                result = db.ExecuteNonQuery(command) > 0;
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            return result;
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        对象添加、修改、查询接口#region 对象添加、修改、查询接口
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 插入指定对象到数据库中
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="obj">指定的对象</param>
ExpandedSubBlockEnd.gif        /// <returns>执行成功返回新增记录的自增长ID。</returns>
InBlock.gif        public bool Insert(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            ArgumentValidation.CheckForNullReference(obj, "传入的对象obj为空");
InBlock.gif            
InBlock.gif            Hashtable hash = GetHashByEntity(obj);
InBlock.gif            return Insert(hash, null);
ExpandedSubBlockEnd.gif        }
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 更新对象属性到数据库中
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="obj">指定的对象</param>
ExpandedSubBlockEnd.gif        /// <returns>执行成功返回<c>true</c>,否则为<c>false</c>。</returns>
InBlock.gif        public bool Update(T obj, string primaryKeyValue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            ArgumentValidation.CheckForNullReference(obj, "传入的对象obj为空");
InBlock.gif        
InBlock.gif            Hashtable hash = GetHashByEntity(obj);
InBlock.gif            return Update(primaryKeyValue, hash, null);
ExpandedSubBlockEnd.gif        }
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 查询数据库,检查是否存在指定ID的对象(用于整型主键)
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="key">对象的ID值</param>
ExpandedSubBlockEnd.gif        /// <returns>存在则返回指定的对象,否则返回Null</returns>
InBlock.gif        public T FindByID(int key)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{            
InBlock.gif            return FindByID(key.ToString());
ExpandedSubBlockEnd.gif        }        
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 查询数据库,检查是否存在指定ID的对象(用于字符型主键)
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="key">对象的ID值</param>
ExpandedSubBlockEnd.gif        /// <returns>存在则返回指定的对象,否则返回Null</returns>
InBlock.gif        public T FindByID(string key)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            string sql = string.Format("Select * From dbo.{0} Where ({1} = @ID)", tableName, primaryKey);
InBlock.gif
InBlock.gif            SqlParameter param = new SqlParameter("@ID", key);
InBlock.gif
InBlock.gif            Database db = DatabaseFactory.CreateDatabase();
InBlock.gif            DbCommand command = db.GetSqlStringCommand(sql);
InBlock.gif            command.Parameters.Add(param);
InBlock.gif
InBlock.gif            T entity = null;
InBlock.gif            using (IDataReader dr = db.ExecuteReader(command))
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                if (dr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
InBlock.gif                    entity = DataReaderToEntity(dr);
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
InBlock.gif            return entity;
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        返回集合的接口#region 返回集合的接口
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 根据ID字符串(逗号分隔)获取对象列表
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="idString">ID字符串(逗号分隔)</param>
ExpandedSubBlockEnd.gif        /// <returns>符合条件的对象列表</returns>
InBlock.gif        public List<T> FindByIDs(string idString)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            string condition = string.Format("{0} in({1})", primaryKey, idString);
InBlock.gif            return this.Find(condition);
ExpandedSubBlockEnd.gif        }        
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 根据条件查询数据库,并返回对象集合
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="condition">查询的条件</param>
ExpandedSubBlockEnd.gif        /// <returns>指定对象的集合</returns>
InBlock.gif        public List<T> Find(string condition)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            //串连条件语句为一个完整的Sql语句
InBlock.gif            string sql = string.Format("Select * From dbo.{0} Where ", tableName);
InBlock.gif            sql +=  condition;
InBlock.gif            sql += string.Format(" Order by {0} {1}", sortField, isDescending ? "DESC" : "ASC"); 
InBlock.gif
InBlock.gif            T entity = null;
InBlock.gif            List<T> list = new List<T>();
InBlock.gif
InBlock.gif            Database db = DatabaseFactory.CreateDatabase();
InBlock.gif            DbCommand command = db.GetSqlStringCommand(sql);
InBlock.gif
InBlock.gif            using (IDataReader dr = db.ExecuteReader(command))
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                while (dr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
InBlock.gif                    entity = DataReaderToEntity(dr);
InBlock.gif
InBlock.gif                    list.Add(entity);
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
InBlock.gif            return list;
ExpandedSubBlockEnd.gif        }
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="condition">查询的条件</param>
InBlock.gif        /// <param name="info">分页实体</param>
ExpandedSubBlockEnd.gif        /// <returns>指定对象的集合</returns>
InBlock.gif        public List<T> Find(string condition, PagerInfo info)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            List<T> list = new List<T>();
InBlock.gif
InBlock.gif            Database db = DatabaseFactory.CreateDatabase();
InBlock.gif
InBlock.gif            PagerHelper helper = new PagerHelper(tableName, condition);
InBlock.gif            info.RecordCount = helper.GetCount();
InBlock.gif
InBlock.gif            PagerHelper helper2 = new PagerHelper(tableName, false, " * ", sortField,
InBlock.gif                info.PageSize, info.CurrenetPageIndex, true, condition);
InBlock.gif
InBlock.gif            using (IDataReader dr = helper2.GetDataReader())
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                while (dr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
InBlock.gif                    list.Add(this.DataReaderToEntity(dr));
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
InBlock.gif            return list;
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 返回数据库所有的对象集合
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <returns>指定对象的集合</returns>
InBlock.gif        public List<T> GetAll()
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            string sql = string.Format("Select * From dbo.{0}", tableName);
InBlock.gif            sql += string.Format(" Order by {0} {1}", sortField, isDescending ? "DESC" : "ASC"); 
InBlock.gif
InBlock.gif            T entity = null;
InBlock.gif            List<T> list = new List<T>();
InBlock.gif
InBlock.gif            Database db = DatabaseFactory.CreateDatabase();
InBlock.gif            DbCommand command = db.GetSqlStringCommand(sql);
InBlock.gif
InBlock.gif            using (IDataReader dr = db.ExecuteReader(command))
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                while (dr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
InBlock.gif                    entity = DataReaderToEntity(dr);
InBlock.gif
InBlock.gif                    list.Add(entity);
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
InBlock.gif            return list;
ExpandedSubBlockEnd.gif        }
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 返回数据库所有的对象集合(用于分页数据显示)
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="info">分页实体信息</param>
ExpandedSubBlockEnd.gif        /// <returns>指定对象的集合</returns>
InBlock.gif        public List<T> GetAll(PagerInfo info)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            List<T> list = new  List<T>();
InBlock.gif            string condition = "";
InBlock.gif
InBlock.gif            Database db = DatabaseFactory.CreateDatabase();
InBlock.gif
InBlock.gif            PagerHelper helper = new PagerHelper(tableName, condition);
InBlock.gif            info.RecordCount = helper.GetCount();
InBlock.gif
InBlock.gif            PagerHelper helper2 = new PagerHelper(tableName, false, " * ", sortField,
InBlock.gif                info.PageSize, info.CurrenetPageIndex, true, condition);
InBlock.gif
InBlock.gif            using (IDataReader dr = helper2.GetDataReader())
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                while (dr.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
InBlock.gif                    list.Add(this.DataReaderToEntity(dr));
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
InBlock.gif            return list;
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif        public DataSet GetAllToDataSet(PagerInfo info)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            DataSet ds = new DataSet();
InBlock.gif            string condition = "";
InBlock.gif
InBlock.gif            PagerHelper helper = new PagerHelper(tableName, condition);
InBlock.gif            info.RecordCount = helper.GetCount();
InBlock.gif
InBlock.gif            PagerHelper helper2 = new PagerHelper(tableName, false, " * ", sortField,
InBlock.gif                info.PageSize, info.CurrenetPageIndex, true, condition);
InBlock.gif
InBlock.gif            return helper2.GetDataSet();
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        子类必须实现的函数(用于更新或者插入)#region 子类必须实现的函数(用于更新或者插入)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 将DataReader的属性值转化为实体类的属性值,返回实体类
InBlock.gif        /// (提供了默认的反射机制获取信息,为了提高性能,建议重写该函数)
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="dr">有效的DataReader对象</param>
ExpandedSubBlockEnd.gif        /// <returns>实体类对象</returns>
InBlock.gif        protected virtual T DataReaderToEntity(IDataReader dr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            T obj = new T();
InBlock.gif            PropertyInfo[] pis = obj.GetType().GetProperties();
InBlock.gif
InBlock.gif            foreach (PropertyInfo pi in pis)
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                try
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
InBlock.gif                    if (dr[pi.Name].ToString() != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
InBlock.gif                        pi.SetValue(obj, dr[pi.Name] ?? "", null);
ExpandedSubBlockEnd.gif                    }
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockStart.gifContractedSubBlock.gif                catch dot.gif{ }
ExpandedSubBlockEnd.gif            }
InBlock.gif            return obj;
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 将实体对象的属性值转化为Hashtable对应的键值(用于插入或者更新操作)
InBlock.gif        /// (提供了默认的反射机制获取信息,为了提高性能,建议重写该函数)
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="obj">有效的实体对象</param>
ExpandedSubBlockEnd.gif        /// <returns>包含键值映射的Hashtable</returns>
InBlock.gif        protected virtual Hashtable GetHashByEntity(T obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            Hashtable ht = new Hashtable();
InBlock.gif            PropertyInfo[] pis = obj.GetType().GetProperties();
InBlock.gif            for (int i = 0; i < pis.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                //if (pis[i].Name != PrimaryKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
InBlock.gif                    object objValue = pis[i].GetValue(obj, null);
InBlock.gif                    objValue = (objValue == null) ? DBNull.Value : objValue;
InBlock.gif
InBlock.gif                    if (!ht.ContainsKey(pis[i].Name))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
InBlock.gif                        ht.Add(pis[i].Name, objValue);
ExpandedSubBlockEnd.gif                    }
ExpandedSubBlockEnd.gif                }
ExpandedSubBlockEnd.gif            }
InBlock.gif            return ht;
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        IBaseDAL接口#region IBaseDAL接口
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 查询数据库,检查是否存在指定键值的对象
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="recordTable">Hashtable:键[key]为字段名;值[value]为字段对应的值</param>
ExpandedSubBlockEnd.gif        /// <returns>存在则返回<c>true</c>,否则为<c>false</c>。</returns>
InBlock.gif        public bool IsExistKey(Hashtable recordTable)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            SqlParameter[] param = new SqlParameter[recordTable.Count];
InBlock.gif            IEnumerator eKeys = recordTable.Keys.GetEnumerator();
InBlock.gif
InBlock.gif            string fields = "";// 字段名
InBlock.gif            int i = 0;
InBlock.gif
InBlock.gif            while (eKeys.MoveNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                string field = eKeys.Current.ToString();
InBlock.gif                fields += string.Format(" {0} = @{1} AND", field, field);
InBlock.gif
InBlock.gif                string val = recordTable[eKeys.Current.ToString()].ToString();
InBlock.gif                param[i] = new SqlParameter(string.Format("@{0}",field), val); 
InBlock.gif
InBlock.gif                i++;
ExpandedSubBlockEnd.gif            }
InBlock.gif
InBlock.gif            fields = fields.Substring(0, fields.Length - 3);//除去最后的AND
InBlock.gif            string sql = string.Format("SELECT COUNT(*) FROM {0} WHERE {1}", tableName, fields);
InBlock.gif
InBlock.gif            Database db = DatabaseFactory.CreateDatabase();
InBlock.gif            DbCommand command = db.GetSqlStringCommand(sql);
InBlock.gif            command.Parameters.AddRange(param);
InBlock.gif
InBlock.gif            return (int)db.ExecuteScalar(command) > 0;
ExpandedSubBlockEnd.gif        }
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 查询数据库,检查是否存在指定键值的对象
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="fieldName">指定的属性名</param>
InBlock.gif        /// <param name="key">指定的值</param>
ExpandedSubBlockEnd.gif        /// <returns>存在则返回<c>true</c>,否则为<c>false</c>。</returns>
InBlock.gif        public bool IsExistKey(string fieldName, object key)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            Hashtable table = new Hashtable();
InBlock.gif            table.Add(fieldName, key);
InBlock.gif
InBlock.gif            return IsExistKey(table);
ExpandedSubBlockEnd.gif        }                        
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 获取数据库中该对象的最大ID值
InBlock.gif        /// </summary>
ExpandedSubBlockEnd.gif        /// <returns>最大ID值</returns>
InBlock.gif        public int GetMaxID()
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            string sql = string.Format("SELECT MAX({0}) AS MaxID FROM dbo.{1}", primaryKey, tableName);
InBlock.gif
InBlock.gif            Database db = DatabaseFactory.CreateDatabase();
InBlock.gif            DbCommand command = db.GetSqlStringCommand(sql);
InBlock.gif
InBlock.gif            object obj = db.ExecuteScalar(command);
InBlock.gif            if(Convert.IsDBNull(obj))
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                return 0;//没有记录的时候为0
ExpandedSubBlockEnd.gif            }
InBlock.gif            return Convert.ToInt32(obj);
ExpandedSubBlockEnd.gif        }
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 根据指定对象的ID,从数据库中删除指定对象(用于整型主键)
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="key">指定对象的ID</param>
ExpandedSubBlockEnd.gif        /// <returns>执行成功返回<c>true</c>,否则为<c>false</c>。</returns>
InBlock.gif        public bool DeleteByKey(string key)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            string condition = string.Format("{0} = '{1}'", primaryKey, key);
InBlock.gif            return DeleteByCondition(condition);
ExpandedSubBlockEnd.gif        }                
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 根据指定条件,从数据库中删除指定对象
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="condition">删除记录的条件语句</param>
ExpandedSubBlockEnd.gif        /// <returns>执行成功返回<c>true</c>,否则为<c>false</c>。</returns>
InBlock.gif        public bool DeleteByCondition(string condition)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            string sql = string.Format("DELETE FROM dbo.{0} WHERE {1} ", tableName, condition);
InBlock.gif
InBlock.gif            Database db = DatabaseFactory.CreateDatabase();
InBlock.gif            DbCommand command = db.GetSqlStringCommand(sql);
InBlock.gif
InBlock.gif            return db.ExecuteNonQuery(command) > 0;
ExpandedSubBlockEnd.gif        }
InBlock.gif                
ExpandedSubBlockEnd.gif        #endregion
ExpandedBlockEnd.gif    }

3、具体的数据访问类
基类完成所有的操作了,对于具体的类将是一大福音,说明它的工作减少很多了,下面看看具体的实现过程。定义一个数据访问类接口,然后实现接口和继承基类即可。

None.gif    public interface IEquipment : IBaseDAL<EquipmentInfo>
ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
ExpandedBlockEnd.gif    }
None.gif    public class Equipment : BaseDAL<EquipmentInfo>, IEquipment
ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        对象实例及构造函数#region 对象实例及构造函数
InBlock.gif
InBlock.gif        public static Equipment Instance
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            get
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                return new Equipment();
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif        public Equipment() : base("Equipment","ID")
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockEnd.gif        #endregion
ExpandedBlockEnd.gif}

其实这样就完成了,我们为了提高效率,重载两个函数的实现,避免基类的属性反射带来的性能损失,这两个函数看似很复杂,其实通过代码生成工具,生成起来也是毫不费功夫的。。

None.gifprotected override EquipmentInfo DataReaderToEntity(IDataReader dataReader)
None.gif
None.gifprotected override Hashtable GetHashByEntity(EquipmentInfo obj)

因此最后的代码就变为下面

None.gif    public class Equipment : BaseDAL<EquipmentInfo>, IEquipment
ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        对象实例及构造函数#region 对象实例及构造函数
InBlock.gif
InBlock.gif        public static Equipment Instance
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            get
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                return new Equipment();
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif        public Equipment() : base("Equipment","ID")
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockEnd.gif        #endregion
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 将DataReader的属性值转化为实体类的属性值,返回实体类
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="dr">有效的DataReader对象</param>
ExpandedSubBlockEnd.gif        /// <returns>实体类对象</returns>
InBlock.gif        protected override EquipmentInfo DataReaderToEntity(IDataReader dataReader)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            EquipmentInfo equipmentInfo = new EquipmentInfo();
InBlock.gif            SmartDataReader reader = new SmartDataReader(dataReader);
InBlock.gif            
InBlock.gif            equipmentInfo.ID = reader.GetInt32("ID");
InBlock.gif            equipmentInfo.PartID = reader.GetString("PartID");
InBlock.gif            equipmentInfo.Name = reader.GetString("Name");
InBlock.gif            equipmentInfo.EquipmentType = reader.GetString("EquipmentType");
InBlock.gif            equipmentInfo.Specification = reader.GetString("Specification");
InBlock.gif            equipmentInfo.Manufacturer = reader.GetString("Manufacturer");
InBlock.gif            equipmentInfo.Picture = reader.GetBytes("Picture");
InBlock.gif            equipmentInfo.ApplyEquipment = reader.GetString("ApplyEquipment");
InBlock.gif            equipmentInfo.BuyAmount = reader.GetInt32("BuyAmount");
InBlock.gif            equipmentInfo.BuyDate = reader.GetDateTime("BuyDate");
InBlock.gif            equipmentInfo.Status = reader.GetString("Status");
InBlock.gif            equipmentInfo.UserName = reader.GetString("UserName");
InBlock.gif            equipmentInfo.SafeNumber = reader.GetInt32("SafeNumber");
InBlock.gif            equipmentInfo.Note = reader.GetString("Note");
InBlock.gif            
InBlock.gif            return equipmentInfo;
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        /// 将实体对象的属性值转化为Hashtable对应的键值
InBlock.gif        /// </summary>
InBlock.gif        /// <param name="obj">有效的实体对象</param>
ExpandedSubBlockEnd.gif        /// <returns>包含键值映射的Hashtable</returns>
InBlock.gif        protected override Hashtable GetHashByEntity(EquipmentInfo obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            EquipmentInfo info = obj as EquipmentInfo;
InBlock.gif            Hashtable hash = new Hashtable(); 
InBlock.gif            
InBlock.gif            hash.Add("ID", info.ID);
InBlock.gif            hash.Add("PartID", info.PartID);
InBlock.gif            hash.Add("Name", info.Name);
InBlock.gif            hash.Add("EquipmentType", info.EquipmentType);
InBlock.gif            hash.Add("Specification", info.Specification);
InBlock.gif            hash.Add("Manufacturer", info.Manufacturer);
InBlock.gif            hash.Add("Picture", info.Picture);
InBlock.gif            hash.Add("ApplyEquipment", info.ApplyEquipment);
InBlock.gif            hash.Add("BuyAmount", info.BuyAmount);
InBlock.gif            hash.Add("BuyDate", info.BuyDate);
InBlock.gif            hash.Add("Status", info.Status);
InBlock.gif            hash.Add("UserName", info.UserName);
InBlock.gif            hash.Add("SafeNumber", info.SafeNumber);
InBlock.gif            hash.Add("Note", info.Note);
InBlock.gif                
InBlock.gif            return hash;
ExpandedSubBlockEnd.gif        }
ExpandedBlockEnd.gif    }



文章太长,下面关于逻辑层、缓存、界面部分的设计在下一篇文章中介绍。

 以上所引用的代码是通过代码生成工具Database2Sharp自动生成(http://www.iqidi.com/Database2Sharp.htm),选择EnterpriseLibrary架构即可。
Database2Sharp_Enterprise.jpg