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可以自定义吗?大家 ...
随机推荐
- [SPOJ VLATTICE]Visible Lattice Points 数论 莫比乌斯反演
7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...
- HDU3535 AreYouBusy 混合背包
题目大意 给出几组物品的体积和价值,每组分为三种:0.组内物品至少选一个:1.组内物品最多选一个:2.组内物品任意选.给出背包容量,求所能得到的最大价值. 注意 仔细审题,把样例好好看完了再答题,否则 ...
- adb命令--之查看进程及Kill进程
adb shell kill [PID] //杀死进程 adb 命令查看程序进程方便简洁高效 adb shell ps //查看所有进程列表,Process Status ad ...
- netpbm开机logo制作工作【转】
本文转载自: http://www.fx114.net/qa-19-74437.aspx
- The Unique MST--hdoj
The Unique MST Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) Tota ...
- Oracle数据库学习1------数据库安装及客户端配置
1.注册Oracle账户: 注册地址:https://login.oracle.com/mysso/signon.jsp 注意:注册的时候尽量使用外国的邮箱,因为使用国内的邮箱可能收不到Oracle发 ...
- NOIP2011 D1T1 铺地毯
P1692 铺地毯 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 NOIP2011 day1 第一题 描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩 ...
- ManualResetEvent和AutoResetEvent的区别,分享来的
在讨论这个问题之前,我们先了解这样一种观点,线程之间的通信是通过发信号来进行沟通的.(这不是废话) 先来讨论ManualResetEvent,讨论过程中我会穿插一些AutoResetEvent的内容, ...
- C#中的值类型、引用类型,代码告诉你他是什么类型。
C#代码告诉你这是什么类型. using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- WPF自定义动画控件 风机
一:创建WPF项目 二:在项目下添加文件Themes,在此文件下添加新项 ”资源词典“取名为 Generic.xaml 注意大小写,之前遇到因为大小写不对应,导致出错的情况Generic.xam ...