我们在后台代码里面操作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. Binding Enum to ComboBox

    1.添加MarkupExtension public class EnumToSourceExtension : MarkupExtension { private Type _type; publi ...

  2. NUnit Console Command Line

    https://github.com/nunit/docs/wiki/Console-Command-Line The console interface runner is invoked by a ...

  3. 深度学习实战篇-基于RNN的中文分词探索

    深度学习实战篇-基于RNN的中文分词探索 近年来,深度学习在人工智能的多个领域取得了显著成绩.微软使用的152层深度神经网络在ImageNet的比赛上斩获多项第一,同时在图像识别中超过了人类的识别水平 ...

  4. 【media-queries】媒体查询,为了响应式设计而生

    目录 简介 语法 常用尺寸 一 简介 针对现在纷杂的设备,css3中加入,可以查询你的浏览类型(screen彩色屏幕, print, all)和css属性判断. 最常用的就是查询屏幕大小,给予适合的展 ...

  5. X264编码实现

    H264 H264的官方测试源码,由德国hhi研究所负责开发.特点:实现了264所有的特性,由于是官方的测试源码,所以学术研究的算法都是在JM基础上实现并和JM进行比较.但其程序结构冗长,只考虑引入各 ...

  6. [原创]Eclipse 安卓开发几个异常的处理办法

    一.代码没有问题,就是报错,重启一下就会好.可以先clean再build; 二.R.Java丢失 网上讲了若干方法,有用android toos的,有clean再build的,我的解决办法是勾选bui ...

  7. 一款APP的开发设计是如何从0到1一步一步设计的

    目前在行业里,关于APP界面设计规范也是层次不齐,很多都还停留在6的设备和ios 9的系统之上,而现在最新的是iphone 7和iOS 10了(更新换代真的很快),我这里说的是最新的iOS 界面设计规 ...

  8. App Store兼容性问题

    app下载出现兼容性问题  项目支持9.0以上的系统 但是10.3的iphone5下载的一直是老版本app  下载时提示不兼容 导致无法正常使用 解决办法: 修改Build-Settings-> ...

  9. go开发和运行环境的配置

    1.运行环境的下载.安装.配置: 下载:http://www.golangtc.com/download  官网下载经常被墙屏蔽,所以就从golang中国下载; 安装及其配置:http://jingy ...

  10. 使用Eric构建Caffe应用程序-Baby年龄识别

    训练好的Caffe网络结构,可以固定下来,直接载入程序作为数据库接口使用.本文使用Eric构建运行于Python环境下的图片识别应用程序,因为Eric使用QT作为GUI,且有Python的接口,可直接 ...