Enterprise Library 5 数据访问之二、自定义RowMapper使用
来源:互联网 发布:浙江企业数据平台 编辑:程序博客网 时间:2024/05/17 03:10
一、应用场景:
由于数据库设计和对象设计并不是一对一的,有时候一张数据表可以拆出多个对象来表述该数据表信息,也有时候多个数据表的某些字段被提取出来对应一个业务对象,这时候自定义RowMapper就有了用武之地。
二、演示代码:
创建一张数据表:User表
if exists (select 1 from sysobjects where id = object_id('"User"') and type = 'U') drop table "User"gocreate table "User" ( Id int identity, Name nvarchar(50) null, Pwd nvarchar(50) null, constraint PK_USER primary key (Id))go创建一个业务实体对象UserInfo:
public class UserInfo { public int Id { set; get; } public string T_UserName { set; get; } public string T_Password { set; get; } }小提示:注意User表和UserInfo对象并非一 一对应哦
现在我们创建一个自定义的 UserInfoMap
public class UserInfoMap : IRowMapper<UserInfo> { public UserInfo MapRow(IDataRecord row) { UserInfo u = new UserInfo() { Id = int.Parse(row.GetString(0)), T_UserName=row.GetString(1), T_Password=row.GetString(2) }; return u; } }自定义数据映射工作已经做完,现在我们去看如何使用我们自定义的RowMapper,贴上核心代码如下:
/// <summary> /// 获取单个对象数据 /// </summary> /// <param name="id">Id</param> /// <returns></returns> public UserInfo GetUserInfo(int id) { string cmdText = string.Format("select * from UserInfo where id={0}", id); var accrssor = db.CreateSqlStringAccessor(cmdText, new UserInfoMap()); var customerData = accrssor.Execute(); return customerData.FirstOrDefault<UserInfo>(); } /// <summary> /// 获取一个对象集合数据 /// </summary> /// <returns></returns> public List<UserInfo> GetUserList() { string cmdText = "select * from UserInfo"; var accrssor = db.CreateSqlStringAccessor<UserInfo>(cmdText, new UserInfoMap());//MapBuilder<UserInfo>.BuildAllProperties() var data = accrssor.Execute(); return data.ToList<UserInfo>(); }
注意:红色字体标记的既是应用了自定义RowMap地方
补充:或者放弃自定义RowMapper可以这样写
public UserInfo GetUserInfo(int id) { string cmdText = string.Format("select * from UserInfo where id={0}", id); var accrssor = db.CreateSqlStringAccessor(cmdText, MapBuilder<UserInfo>.MapAllProperties() .Map(x => x.UserName).ToColumn("UserName") .Map(x => x.Password).ToColumn("Password") .Build() ); var customerData = accrssor.Execute(); return customerData.FirstOrDefault<UserInfo>(); }
至此结束。欢迎拍砖.........
- Enterprise Library 5 数据访问之二、自定义RowMapper使用
- Enterprise Library 之数据加密
- Enterprise Library 数据访问模块的事务处理
- Enterprise Library 4 数据访问应用程序块
- Enterprise Library 2.0 Hands On Lab 翻译(2):数据访问程序块(二)
- Enterprise Library 4.1数据访问应用程序块快速入门【1】使用DbDataReader检索多行
- Enterprise Library 4.1数据访问应用程序块快速入门【2】使用DataSet检索多行
- Enterprise Library 4.1数据访问应用程序块快速入门【6】使用DataSet更新数据库
- Enterprise Library 5 数据访问之一(初级)体验篇:直接返回业务对象数据
- Enterprise Library之日志。
- Enterprise Library 之缓存
- Enterprise Library 使用
- Enterprise Library 4.0中文文档-数据访问应用程序块【1】数据访问应用程序块简介
- Enterprise Library 4.0中文文档-数据访问应用程序块【2】数据访问应用程序块简介
- Enterprise Library 4.1数据访问应用程序块快速入门【5】在事务内执行多个更新程序
- Enterprise Library 4.1数据访问应用程序块快速入门【3】执行命令和访问输出参数
- Enterprise Library连接Access数据
- 2.1 使用 Enterprise Library 二进制
- 使用Xapian搭建自己的搜索引擎
- Matlab梯度法图像锐化
- Ubuntu 10.04下完全卸载firefox3.6方法
- CXF wsdl2Java
- Path Manipulation Routines
- Enterprise Library 5 数据访问之二、自定义RowMapper使用
- Windows与Linux实现共享方法总结
- Xapian 术语表
- Ubuntu 下查看系统版本号
- 开源授权协议GPL LGPL BSD介绍
- Ubuntu 下比较好的chm阅读软件
- 2011.7.11
- Python 之禅,by Tim Peters
- XAPIAN学习1--倒排数据库 建立,工厂模式应用