petapoco IsNew
// Check if a poco represents a new record
public bool IsNew(string primaryKeyName, object poco)
{
var pd = PocoData.ForObject(poco, primaryKeyName);
object pk;
PocoColumn pc;
if (pd.Columns.TryGetValue(primaryKeyName, out pc))
{
pk = pc.GetValue(poco);
}
#if !PETAPOCO_NO_DYNAMIC
else if (poco.GetType() == typeof(System.Dynamic.ExpandoObject))
{
return true;
}
#endif
else if (primaryKeyName.Contains(","))
{
return primaryKeyName.Split(',')
.Select(pkPart => GetValue(pkPart, poco))
.Any(pkValue => IsDefaultOrNull(pkValue));
}
else
{
pk = GetValue(primaryKeyName, poco);
}
return IsDefaultOrNull(pk);
}
private static object GetValue(string primaryKeyName, object poco)
{
object pk;
var pi = poco.GetType().GetProperty(primaryKeyName);
if (pi == null)
throw new ArgumentException(
string.Format("The object doesn't have a property matching the primary key column name '{0}'",
primaryKeyName));
pk = pi.GetValue(poco, null);
return pk;
}
private static bool IsDefaultOrNull(object pk)
{
if (pk == null)
return true;
var type = pk.GetType();
if (type.IsValueType)
{
// Common primary key types
if (type == typeof(long))
return (long)pk == default(long);
else if (type == typeof(ulong))
return (ulong)pk == default(ulong);
else if (type == typeof(int))
return (int)pk == default(int);
else if (type == typeof(uint))
return (uint)pk == default(uint);
else if (type == typeof(Guid))
return (Guid)pk == default(Guid);
// Create a default instance and compare
return pk == Activator.CreateInstance(pk.GetType());
}
else
{
return pk == null;
}
}
petapoco IsNew的更多相关文章
- Mini ORM——PetaPoco笔记
Mini ORM--PetaPoco笔记 记录一下petapoco官网博客的一些要点.这些博客记录了PetaPoco是如何一步步改进的. 目录: Announcing PetaPoco PetaPoc ...
- PetaPoco 使用总结(一)
PetaPoco 使用总结(一) 前段时间,公司的一个项目希望用一个ORM 的框架,通过对比 Dapper 和 PetaPoco ,虽然Dapper 功能很强大,速度更快. 但是最终还是选择了比较简单 ...
- Mini ORM——PetaPoco笔记(转)
记录一下petapoco官网博客的一些要点.这些博客记录了PetaPoco是如何一步步改进的. 目录: Announcing PetaPoco PetaPoco-Improvements PetaPo ...
- 【译】微型ORM:PetaPoco【不完整的翻译】
PetaPoco是一款适用于.Net 和Mono的微小.快速.单文件的微型ORM. PetaPoco有以下特色: 微小,没有依赖项……单个的C#文件可以方便的添加到任何项目中. 工作于严格的没有装饰的 ...
- PetaPoco入门(二)
1. Petapoco基本用法 1.1. 创建示例工程 首先创建一个工程文件,为了便于展示数据这里创建一个类型为:WindowsApplication的工程文件.命名为:PetapocoTest. 程 ...
- PetaPoco入门(一)
1. ORM概括 1.1. ORM简介 ORM 对象-关系映射(Object/Relation Mapping,简称ORM),是随着面向对象的软件开发方法发展而产生的.面向对象的开发方法是当今企业级应 ...
- PetaPoco 笔记
PetaPoco是一款适用于.Net 和Mono的微小.快速.单文件的微型ORM. PetaPoco有以下特色: 微小,没有依赖项--单个的C#文件可以方便的添加到任何项目中. 工作于严格的没有装饰的 ...
- C#轻型ORM框架PetaPoco试水
近端时间从推酷app上了解到C#轻微型的ORM框架--PetaPoco.从github Dapper 开源项目可以看到PetaPoco排第四 以下是网友根据官方介绍翻译,这里贴出来. PetaPoco ...
- ORM之PetaPoco入门(二)--Petapoco基本用法
1. Petapoco基本用法 1.1. 创建示例工程 首先创建一个工程文件,为了便于展示数据这里创建一个类型为:WindowsApplication的工程文件.命名为:PetapocoTest. 程 ...
随机推荐
- centos 防火墙配置
转载:http://www.cnblogs.com/bugs/p/3587915.html 1.安装iptables防火墙 怎么知道系统是否安装了iptables?执行iptables -V,如果显示 ...
- blocked file type by sharepoint 分类: Sharepoint 2015-07-05 07:45 6人阅读 评论(0) 收藏
o add or remove blocked file types by using Central Administration Verify that you have the followin ...
- C++二进制文件中读写bitset
这个比较简单,直接上代码: bitset< > *b = >(); bitset< > *c = >(); ofstream out("I:\\test. ...
- 设计模式之二:MVC
模型(Model) 视图(View) 控制器(Controller) (MVC) 是 Cocoa 中的一种行为模块,并且也是所有 Cocoa 设计模式中使用最多的.在程序中按照它们的角色来分类这些对象 ...
- html,if标签使用
$vo.auth_id eq $vo2.auth_pid报错不能用 condition里面访问变量,不能用点的形式,应该用['']的形式访问
- OD调试篇13
今天的程序比较大,听说还是比较牛的程序,不过破解它的一个比较老的版本的.打开程序看看. 点击about 然后点enter registration code 输入name 以及密钥之后,会 ...
- 地图和定位 、 iCloud
1 FindMe应用 1.1 问题 MapKit框架可以用于创建现场交互的地图来显示用户想要设备显示的任何位置,包括用户的当前位置,甚至可以进行标记并查看地图上的标注信息.CoreLocation框架 ...
- LintCode First Position of Target
找指定target的最左位置. class Solution { /** * @param nums: The integer array. * @param target: Target to fi ...
- kernel/Makefile
## Makefile for the linux kernel.## Note! Dependencies are done automagically by 'make dep', which a ...
- 使用STL离散化
把原来的数组a复制一份拷贝b 用sort先把数组a排序 用unique消除a里面重复的元素 对于b中的每一个元素,用lower_bound找到它在a中的位置,也就是离散化之后的编号. 没了. #inc ...