crm高速开发之Entity
我们在后台代码里面操作Entity的时候,基本上是这样写的:
/* 创建者:菜刀居士的博客
* 创建日期:2014年07月5号
*/
namespace Net.CRM.Entity
{
using System;
using Microsoft.Xrm.Sdk;
/// <summary>
/// 基本模式---Entity
/// </summary>
public class EntityDemo
{
public void Run(Entity entity)
{
if (IsNotNull(entity,"new_int"))
{
//获取int类型的字段的值
int new_int = entity.GetAttributeValue<int>("new_int");
}
if (IsNotNull(entity, "new_string"))
{
//获取string类型的字段的值
string new_string = entity.GetAttributeValue<string>("new_string");
}
if (IsNotNull(entity, "new_float"))
{
//获取float类型的字段的值
float new_float = entity.GetAttributeValue<float>("new_float");
}
if (IsNotNull(entity, "new_money"))
{
//获取Money(货币)类型的字段的值
decimal new_money = entity.GetAttributeValue<Money>("new_money").Value;
}
if (IsNotNull(entity, "new_picklist"))
{
//获取OptionSetValue(下拉框)类型的字段的值
int new_picklist = entity.GetAttributeValue<OptionSetValue>("new_picklist").Value;
}
if (IsNotNull(entity, "new_lookup"))
{
//获取EntityReference(引用字段)类型的字段的值
EntityReference new_lookup = entity.GetAttributeValue<EntityReference>("new_lookup");
}
}
/// <summary>
/// 推断实体的某个字段是否为空
/// </summary>
/// <param name="entity">实体</param>
/// <param name="name">字段名称</param>
public bool IsNotNull(Entity entity,string name)
{
return entity.Contains(name) && entity.Attributes[name] != null;
}
}
}
这样写。是正确的,可是。非常繁琐,以下是高速的写法:
/* 创建者:菜刀居士的博客
* 创建日期:2014年07月5号
*/
namespace Net.CRM.Entity
{
using System;
using Microsoft.Xrm.Sdk;
/// <summary>
/// 高速开发---Entity
/// </summary>
public class EntityQuickDemo
{
public void Run(Entity entity)
{
if (entity.IsNotNull("new_int"))
{
//获取int类型的字段的值
int new_int = entity.ToInt("new_int");
}
if (entity.IsNotNull("new_string"))
{
//获取string类型的字段的值
string new_string = entity.ToString("new_string");
}
if (entity.IsNotNull("new_float"))
{
//获取float类型的字段的值
float new_float = entity.ToFloat("new_float");
}
if (entity.IsNotNull("new_money"))
{
//获取Money(货币)类型的字段的值
decimal new_money = entity.ToMoney("new_money");
}
if (entity.IsNotNull("new_picklist"))
{
//获取OptionSetValue(下拉框)类型的字段的值
int new_picklist = entity.ToOpInt("new_picklist");
}
if (entity.IsNotNull("new_lookup"))
{
//获取EntityReference(引用字段)类型的字段的值
EntityReference new_lookup = entity.ToEr("new_lookup");
}
}
}
/// <summary>
/// 扩展方法
/// </summary>
public static class EntityFunction
{
/// <summary>
/// Int
/// </summary>
public static int ToInt(this Entity entity, string name)
{
return entity.GetAttributeValue<int>(name);
}
/// <summary>
/// string
/// </summary>
public static string ToString(this Entity entity, string name)
{
return entity.GetAttributeValue<string>(name);
}
/// <summary>
/// float
/// </summary>
public static float ToFloat(this Entity entity, string name)
{
return entity.GetAttributeValue<float>(name);
}
/// <summary>
/// Money
/// </summary>
public static decimal ToMoney(this Entity entity, string name)
{
return entity.GetAttributeValue<Money>(name).Value;
}
/// <summary>
/// OptionSetValue
/// </summary>
public static int ToOpInt(this Entity entity, string name)
{
return entity.GetAttributeValue<OptionSetValue>(name).Value;
}
/// <summary>
/// EntityReference
/// </summary>
public static EntityReference ToEr(this Entity entity, string name)
{
return entity.GetAttributeValue<EntityReference>(name);
}
public static T GetAttributeValue<T>(this Entity entity, string name)
{
if (entity.IsNotNull(name))
{
return entity.GetAttributeValue<T>(name);
}
return default(T);
}
/// <summary>
/// 推断实体的某个字段是否为空
/// </summary>
/// <param name="entity">实体</param>
/// <param name="name">字段名称</param>
public static bool IsNotNull(this Entity entity, string name)
{
return entity.Contains(name) && entity.Attributes[name] != null;
}
}
}
crm高速开发之Entity的更多相关文章
- crm高速开发之EntityCollection
/* 创建者:菜刀居士的博客 * 创建日期:2014年07月07号 */ namespace Net.CRM.OrganizationService { using System; ...
- crm高速开发之OrganizationService
这是主要的开发模式: /* 创建者:菜刀居士的博客 * 创建日期:2014年07月06号 */ namespace Net.CRM.OrganizationService { using ...
- crm高速开发之QueryExpression
/* 创建者:菜刀居士的博客 * 创建日期:2014年07月06号 */ namespace Net.CRM.OrganizationService { using System; ...
- 8天掌握EF的Code First开发之Entity Framework介绍
返回<8天掌握EF的Code First开发>总目录 本篇目录 Entity Framework概要 什么是ORM Entity Framework简史 Entity Framework具 ...
- 转载 8天掌握EF的Code First开发之Entity Framework介绍
转载原地址: http://www.cnblogs.com/farb/p/IntroductionToEF.html Entity Framework概要 Entity Framework是微软的O ...
- Liferay7 BPM门户开发之37: Liferay7下的OSGi Hook集成开发
hook开发是Liferay客制扩展的一种方式,比插件灵活,即可以扩展liferay门户,也能对原有特性进行更改,Liferay有许多内置的服务,比如用hook甚至可以覆盖Liferay服务. 可作为 ...
- JavaEE开发之SpringMVC中的自定义拦截器及异常处理
上篇博客我们聊了<JavaEE开发之SpringMVC中的路由配置及参数传递详解>,本篇博客我们就聊一下自定义拦截器的实现.以及使用ModelAndView对象将Controller的值加 ...
- [Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘终结篇:UniLua热更新全然解读
---------------------------------------------------------------------------------------------------- ...
- 高效开发之SASS篇 灵异留白事件——图片下方无故留白 你会用::before、::after吗 link 与 @import之对比 学习前端前必知的——HTTP协议详解 深入了解——CSS3新增属性 菜鸟进阶——grunt $(#form :input)与$(#form input)的区别
高效开发之SASS篇 作为通往前端大神之路的普通的一只学鸟,最近接触了一样稍微高逼格一点的神器,特与大家分享~ 他是谁? 作为前端开发人员,你肯定对css很熟悉,但是你知道css可以自定义吗?大家 ...
随机推荐
- 【Codeforces 258A】 Game With Sticks
[题目链接] http://codeforces.com/contest/451/problem/A [算法] 若n和m中的最小值是奇数,则先手胜,否则后手胜 [代码] #include<bit ...
- ubantu下NiFi单节点安装部署
第一步,首先下载安装包:http://nifi.apache.org/download.html,博主下载的是1.4.0版本,直接下载的是编译后的文件. 第二步:将压缩包上传到服务器相应目录下,并且解 ...
- GStreamer基础教程01 - Hello World
摘要 在面对一个新的软件库时,第一步通常实现一个“hello world”程序,来了解库的用法.对于GStreamer,我们可以实现一个极简的播放器,来了解GStreamer的使用. 环境配置 为了快 ...
- jQuery基本选择器模块
选择器模块 1.获取元素的基本操作 案例:给页面中的div和p设置边框样式 1.1 传统方式 -获取元素并设置样式 实现思路 1 通过 标签名 获取元素 2 遍历循环 设置样式 var dvs = d ...
- JQuery学习笔记系列(二)----
jQuery是一个兼容多浏览器的javascript库,核心理念是write less,do more(写得更少,做得更多).其中也提供了很多函数来更加简洁的实现复杂的功能. 事件切换函数toggle ...
- 【Oracle】闪回drop后的表
本文介绍的闪回方式只适用于:删除表的表空间非system,drop语句中没有purge关键字(以上两种情况的误删除操作只能通过日志找回): 1.删除表后直接从回收站闪回 SCOTT@LGR> d ...
- js手机网络检测
<!DOCTYPE HTML> <html lang="en"> <head> <meta charset=UTF-8"> ...
- ML:流形学习
很多原理性的东西需要有基础性的理解,还是篇幅过少,所以讲解的不是特别的清晰. 原文链接:http://blog.sciencenet.cn/blog-722391-583413.html 流形(man ...
- CSS字体代码
宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMingLiU 细明体 Ming ...
- PhotoZoom官方这举动,大写服!
上上周,PhotoZoom Classic7首次特惠活动大家都知道哈~~ 厂商福利限量30套,仅售99RMB,活动一经上线,半天时间一售而光,这说明不是大家不需要这个智能小软件啊,而是,可能,大概,也 ...