我们在后台代码里面操作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的更多相关文章

  1. crm高速开发之EntityCollection

    /* 创建者:菜刀居士的博客  * 创建日期:2014年07月07号  */ namespace Net.CRM.OrganizationService {     using System;     ...

  2. crm高速开发之OrganizationService

    这是主要的开发模式: /* 创建者:菜刀居士的博客  * 创建日期:2014年07月06号  */ namespace Net.CRM.OrganizationService {     using ...

  3. crm高速开发之QueryExpression

    /* 创建者:菜刀居士的博客  * 创建日期:2014年07月06号  */ namespace Net.CRM.OrganizationService {     using System;     ...

  4. 8天掌握EF的Code First开发之Entity Framework介绍

    返回<8天掌握EF的Code First开发>总目录 本篇目录 Entity Framework概要 什么是ORM Entity Framework简史 Entity Framework具 ...

  5. 转载 8天掌握EF的Code First开发之Entity Framework介绍

    转载原地址:  http://www.cnblogs.com/farb/p/IntroductionToEF.html Entity Framework概要 Entity Framework是微软的O ...

  6. Liferay7 BPM门户开发之37: Liferay7下的OSGi Hook集成开发

    hook开发是Liferay客制扩展的一种方式,比插件灵活,即可以扩展liferay门户,也能对原有特性进行更改,Liferay有许多内置的服务,比如用hook甚至可以覆盖Liferay服务. 可作为 ...

  7. JavaEE开发之SpringMVC中的自定义拦截器及异常处理

    上篇博客我们聊了<JavaEE开发之SpringMVC中的路由配置及参数传递详解>,本篇博客我们就聊一下自定义拦截器的实现.以及使用ModelAndView对象将Controller的值加 ...

  8. [Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘终结篇:UniLua热更新全然解读

    ---------------------------------------------------------------------------------------------------- ...

  9. 高效开发之SASS篇 灵异留白事件——图片下方无故留白 你会用::before、::after吗 link 与 @import之对比 学习前端前必知的——HTTP协议详解 深入了解——CSS3新增属性 菜鸟进阶——grunt $(#form :input)与$(#form input)的区别

    高效开发之SASS篇   作为通往前端大神之路的普通的一只学鸟,最近接触了一样稍微高逼格一点的神器,特与大家分享~ 他是谁? 作为前端开发人员,你肯定对css很熟悉,但是你知道css可以自定义吗?大家 ...

随机推荐

  1. 几款jQuery右键菜单插件介绍

    在网页中使用自定义右键菜单,实现上皆为使用javascript禁用浏览器默认的右键菜单,然后在网页中响应鼠标右键事件,弹出自定义的菜单. 类似右键菜单的组件网上很多.一般而言,改变浏览器的默认菜单应当 ...

  2. ROS-tutorials-launch-查看日志

    前言:launch文件的作用是一次可以启动多个节点. 1.新建launch文件 在chapter2_tutorials包下新建launch文件夹,并在launch文件夹下新建chapter2.laun ...

  3. C - Valera and Fruits

    Problem description Valera loves his garden, where n fruit trees grow. This year he will enjoy a gre ...

  4. java selenium手动最大化chrome浏览器的方法

    package my_automation; import java.awt.Dimension; import org.openqa.selenium.Capabilities; import or ...

  5. 原生js实现简单JSONP

    JSONP是一种非常常见的实现跨域请求的方法.其基本思想是利用浏览器中可以跨域请求外链的JS文件,利用这一特性实现数据传输. 用原生JS实现JSONP非常简单,无非几点: 1)定义一个函数,用于处理接 ...

  6. 企业级Spring应用的搭建

    本次博客将要对SpringMVC做简单的介绍以及环境的搭建: 概述 Spring 框架是一个开源的平台,属于设计层面框架,整个系统面向接口,是分层的JavaSE/EE开源框架,用于解决复杂的企业应用开 ...

  7. <Android Framework 之路>Android5.1 MediaScanner

    前言 MediaScanner是Android系统中针对媒体文件的扫描过程,将储存空间中的媒体文件通过扫描的方式遍历并存储在数据库中,然后通过MediaProvider提供接口使用,在Android多 ...

  8. 最简单的一致性Hash算法实现

    import java.util.Collection;import java.util.SortedMap;import java.util.TreeMap; public class Consis ...

  9. 【原创】IBM Websphere 报错:JSPG0120E: 为 pageEncoding 属性和匹配 URI 模式的配置元素指定不同的值是非法的。

    websphere中间件,在打开一个jsp页面时报: IBM Websphere 报错:JSPG0120E: 为 pageEncoding 属性和匹配 URI 模式的配置元素指定不同的值是非法的. . ...

  10. java加密解密算法位运算

    一.实例说明 本实例通过位运算的异或运算符 “ ^ ” 把字符串与一个指定的值进行异或运算,从而改变每个字符串中字符的值,这样就可以得到一个加密后的字符串.当把加密后的字符串作为程序输入内容,异或运算 ...