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;

}

}

}

时间: 2024-07-31 17:46:23

crm高速开发之Entity的相关文章

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 (

crm高速开发之EntityCollection

/* 创建者:菜刀居士的博客 ?* 创建日期:2014年07月07号 ?*/ namespace Net.CRM.OrganizationService { ??? using System; ??? using Microsoft.Xrm.Sdk; ??? using Microsoft.Xrm.Sdk.Query; ??? /// <summary> ??? /// EntityCollection ??? /// </summary> ??? public class Ent

crm快速开发之OrganizationService

这是基本的开发模式: /* 创建者:菜刀居士的博客 * 创建日期:2014年07月06号 */ namespace Net.CRM.OrganizationService { using System; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; /// <summary> /// 基本模式---OrganizationService /// </summary> public class Organization

crm快速开发之EntityCollection

/* 创建者:菜刀居士的博客 * 创建日期:2014年07月07号 */ namespace Net.CRM.OrganizationService { using System; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; /// <summary> /// EntityCollection /// </summary> public class EntityCollectionDemo { /// <su

crm快速开发之QueryExpression

/* 创建者:菜刀居士的博客 * 创建日期:2014年07月06号 */ namespace Net.CRM.OrganizationService { using System; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using System.Collections.Generic; /// <summary> /// QueryExpression /// </summary> public class

具体解释EBS接口开发之WIP模块接口

整体说明 文档目的 本文档针对WIP模块业务功能和接口进行分析和研究,对採用并发请求方式和调用API方式分别进行介绍 内容 WIP模块经常使用标准表简单介绍 WIP事物处理组成 WIP相关业务流程 WIP相关API研究事例 (十)參考文档(七)採购相关的一些知识 (一)WIP模块经常使用标准表简单介绍 1.1   经常使用标准表 例如以下表中列出了与WIP导入相关的表和说明: 表名 说明 其它信息 BOM_STRUCTURES_B BOM头信息 BOM_COMPONENTS_B BOM组件信息

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

hook开发是Liferay客制扩展的一种方式,比插件灵活,即可以扩展liferay门户,也能对原有特性进行更改,Liferay有许多内置的服务,比如用hook甚至可以覆盖Liferay服务. 可作为系统服务挂钩(Liferay Service Hook),还有其他类型的hook... Liferay6.2 时的hook开发比较有限,而在Liferay7则大为不同,OSGi services的彻底改进至Liferay的底层模型框架,使得Liferay可以支持更多的定制扩展!OSGi plugin

详解EBS接口开发之WIP模块接口

总体说明 文档目的 本文档针对WIP模块业务功能和接口进行分析和研究,对采用并发请求方式和调用API方式分别进行介绍 内容 WIP模块常用标准表简介 WIP事物处理组成 WIP相关业务流程 WIP相关API研究事例 (十)参考文档(七)采购相关的一些知识 (一)WIP模块常用标准表简介 1.1   常用标准表 如下表中列出了与WIP导入相关的表和说明: 表名 说明 其他信息 BOM_STRUCTURES_B BOM头信息 BOM_COMPONENTS_B BOM组件信息 BOM_OPERATIO

IOS开发之copy的问题

copy的目的就是修改副本,修改原始对象和副本时不会产生干扰. 定义一个不可变属性A,再定义一个可变属性B.用B做添加删除等操作后再将B赋值给A时,有些人习惯用A = B:其实这样是不安全的. 假设有下面的一段代码: ? 1 2 3 4 5 6 7 8 9 10   int main() {    NSMutableString *strM = [NSMutableString [email protected]"123"];    NSString *str = strM;    N