该实体实现了IEntityBase接口的三个属性定义,该实体已经彻底摒弃了反射方法
命名空间:DevNet.Entity
这里列举几个重要的方法签名:
折叠展开C# Code复制内容到剪贴板
-
- ,
-
- protected void IniMember(string[] fieldNames, object[] fieldValues)
-
-
-
-
-
-
- protected void SetProperty(string fieldName, object fieldValue)
-
-
-
-
-
-
- protected object GetProperty(string fieldName)
-
-
-
-
-
-
- public object this[string fieldName]
-
以上为几个比较重要的基类方法
我们来看一下该实体如何摒弃反射方法
从DataReader转换为实体集合时的方法如下,以下方法在DevNet.DBAcces.DBHelper类中:
折叠展开C# Code复制内容到剪贴板
-
-
-
-
-
-
- public static List<T> GetEntityLists<T>(DbDataReader dbRead) where T : EntityBase, new()
- {
- if (dbRead == null)
- throw new ArgumentNullException("dbRead", "参数dbRead不能为null");
- List<T> ts = new List<T>();
- while (dbRead.Read())
- {
- T t = new T();
- SetEntityByRead(t, dbRead);
- ts.Add(t);
- }
- return ts;
- }
接下来看一下SetEntityByRead方法:
折叠展开C# Code复制内容到剪贴板
-
-
-
-
-
-
- public static void SetEntityByRead<T>(T objEntity, DbDataReader dbRead) where T : EntityBase
- {
- if (objEntity == null) throw new ArgumentNullException("objEntity");
- if (dbRead == null) throw new ArgumentNullException("dbRead");
-
- int i = dbRead.FieldCount;
- for(int j=0;j<i;j++)
- {
- if (!dbRead.IsDBNull(j))
- {
- objEntity.SetPropertyValue(dbRead.GetName(j), dbRead[j]);
- }
- }
- }
通过以上两个方法,相信大家也了解了该实体设计方法,它已经摒弃了使用反射的方法
DBHelper类为实体数据帮助类 [提供object对象[实体类]、DataTable、DbDataReader和DbParameter与之间的转换设置],命名空间为:DevNet.DBAccess.DBHelper