您的位置:首页 > 编程语言 > C#

C#教程之自己动手写映射第六节[封装列表]

2012-08-21 10:36 141 查看
一、动机

  经过了前面几节的阐述,我们已经可以通过"动态生成SQL"与"反射机制"完成简单的对象与数据表的映射。如:Add(object obj)、Remove(object obj)等。好的,我们看如下代码:

public static List<Model.A> GetList(int PageSize, int CurrentPageIndex, out int TotalCount)
{
List<Model.A> listA = new List<Model.A>();
DataSet ds = SqlCommon.GetList(conn, "ID", PageSize, PageSize * (CurrentPageIndex - 1), "A", "1=1", out TotalCount);
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
Model.A employee = new Model.A();
employee.ID = Convert.ToInt32(dr["ID"]);
employee.Name = dr["Name"].ToString();
employee.Password = dr["Password"].ToString();
employee.Department = dr["Department"].ToString();
employee.Position = dr["Position"].ToString();
listA.Add(employee);
}
}
return listA;
}

public static List<Model.B> GetList(int PageSize, int CurrentPageIndex, out int TotalCount)
{
List<Model.B> listB = new List<Model.B>();
DataSet ds = SqlCommon.GetList(conn, "ID", PageSize, PageSize * (CurrentPageIndex - 1), "B", "1=1", out TotalCount);
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
Model.B employee = new Model.B();
employee.ID = Convert.ToInt32(dr["ID"]);
employee.Name = dr["Name"].ToString();
employee.Password = dr["Password"].ToString();
employee.Department = dr["Department"].ToString();
employee.Position = dr["Position"].ToString();
listB.Add(employee);
}
}
return listB;
}


  我们在获取列表的时候总会对DataSet或DataReader里的数据进行转换,并放入到DTO中。

[b]二、构想[/b]

[b]  [/b]我们是否可以把重复书写的代码进行封装并对外提供接口以简化我们的工作量呢...如下所示:

public static List<object> GetList(object classObject, string AssemblyName, string ConnString);
public static List<object> GetList(object classObject, string strWHERE, string AssemblyName, string ConnString);
public static List<object> GetList(object classObject, int intPageSize, int intCurrentCount, out int intTotalCount, string AssemblyName, string ConnString);
public static List<object> GetList(object classObject, int intPageSize, int intCurrentCount, string strWhere, out int intTotalCount, string AssemblyName, string ConnString);


  这样我们再通过类型转换就可以使用List了.

[b][b]三、实现[/b][/b]

[b][b]  [/b][/b]我们依然是依赖对象映射去转换DataSet,如下代码所示:

/// <summary>
/// 把DataSet 转换成为List
/// </summary>
/// <param name="ds"></param>
/// <param name="type"></param>
/// <param name="BaseFieldMapping"></param>
/// <returns></returns>
internal static List<object> ChangeDateSetToList(object classObject, Dictionary<string, string> FieldMapping, DataSet ds)
{
//获取传入的类型
Type type = classObject.GetType();
//创建返回值
List<object> tempList = new List<object>();

//检查DataSet的Table和Rows是否存在,至少取出一条数据
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
//获取属性名称及类型字典
PropertyInfo[] properInfo = type.GetProperties();
Dictionary<string, Type> proDictionary = new Dictionary<string, Type>();
foreach (PropertyInfo proper in properInfo)
{
proDictionary.Add(proper.Name, proper.PropertyType);
}

//遍历DataSet
int intRowCount = ds.Tables[0].Rows.Count;
for (int j = 0; j < intRowCount; j++)
{
//创建一个传入类型的新的实例
object typeTempObject = type.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
//属性赋值
foreach (string strKey in FieldMapping.Keys)
{
//根据数据类型取出值
object[] arrObject = Model.GetProType(classObject.GetType().GetProperty(strKey).PropertyType, ds.Tables[0].Rows[j][FieldMapping[strKey]].ToString());
//属性赋值
type.InvokeMember(strKey, BindingFlags.SetProperty, null, typeTempObject, arrObject);
}
//将实体添加到 List
tempList.Add(typeTempObject);
//清空对象引用
typeTempObject = null;
}
}
return tempList;
}


四、版权

  转载请注明出处:http://www.cnblogs.com/iamlilinfeng
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: