简单的数据库表到对象的ORM映射
来源:互联网 发布:达内大数据培训学费 编辑:程序博客网 时间:2024/05/29 23:23
根据微软企业库中数据访问模块改造的一个对象映射类,针对DataRow进行扩展,提供方便的DataTable到对象集合的映射。
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;public static class DataRowExtensions{ /// <summary> /// 通过数据行创建一个对象 /// </summary> public static T MapToObject<T>(this DataRow dataRow) { Type type = typeof(T); T convertedObject = System.Activator.CreateInstance<T>(); MapToObject(dataRow, convertedObject); return convertedObject; } /// <summary> /// 通过行集合创建一个对象列表 /// </summary> public static List<T> MapToList<T>(this DataRowCollection dataRowCollection) { Type type = typeof(T); List<T> objects = new List<T>(); foreach (DataRow dataRow in dataRowCollection) { objects.Add(MapToObject<T>(dataRow)); } return objects; } /// <summary> /// 通过数据行映射现有对象 /// </summary> public static void MapToObject(this DataRow dataRow, object convertedObject) { Type objectType = convertedObject.GetType(); System.Reflection.PropertyInfo[] properties = objectType.GetProperties(); foreach (System.Reflection.PropertyInfo property in properties) { if (dataRow.Table.Columns.Contains(property.Name)) { object value = dataRow[property.Name]; object convertedValue = ConvertValue(value, property.PropertyType); property.SetValue(convertedObject, convertedValue, new object[0]); } } } public static object ConvertValue(object value, Type conversionType) { if (IsNullableType(conversionType)) { return ConvertNullableValue(value, conversionType); } return ConvertNonNullableValue(value, conversionType); } private static bool IsNullableType(Type t) { return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>); } public static object ConvertNonNullableValue(object value, Type conversionType) { object convertedValue = null; if (conversionType.IsEnum) { conversionType = Enum.GetUnderlyingType(conversionType); } if (value != DBNull.Value) { convertedValue = Convert.ChangeType(value, conversionType); } return convertedValue; } public static object ConvertNullableValue(object value, Type conversionType) { if (value != DBNull.Value) { var converter = new NullableConverter(conversionType); return converter.ConvertFrom(value); } return null; }}
阅读全文
0 0
- 简单的数据库表到对象的ORM映射
- ORM中对象与数据库表之间的映射机制
- ORM建立类->数据库表的映射
- ActiveAndroid 简单方便的ORM(对象关系映射)
- android中ORM(对象关系映射)数据库LitePal的使用
- android中ORM(对象关系映射)数据库greenDAO的使用
- 数据库的表映射到对象上真的不是那么简单啊!
- 对ORM对象关系映射的理解
- Android数据库框架——ORMLite轻量级的对象关系映射(ORM)Java包
- Android数据库框架——ORMLite轻量级的对象关系映射(ORM)Java
- 详解python django面向关系数据库的ORM对象映射系统(1)
- ORM——数据库表与Java对象映射原理
- hibernate的ORM映射
- 对象关系映射(ORM)简单看懂
- 浅谈ORM对象关系数据库映射
- 面向对象的数据库开发--再论ORM
- 业务对象到关系数据库映射的若干模式
- ORM框架分析之DB到Entity的映射(一)
- JNDI学习总结(一)——JNDI数据源的配置
- 剑指offer:输入一个链表,输出该链表中倒数第k个结点。
- vue.js和mvvm小细节
- 微信小程序swiper组件实现图片宽度自适应
- Log4j 配置详解
- 简单的数据库表到对象的ORM映射
- MySQL
- NoSuchElementException
- hash算法及Java的HashTable源码分析
- 【HTTP】Fiddler(一)- Fiddler简介
- 如何获取input中输入的值
- PHP服务器配置php.ini
- recovery 升级过程LED灯闪烁
- retrofit中异常com.google.gson.JsonSyntaxException解决方案