• 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 OrganizationServiceDemo
        {
            /// <summary>
            /// 查询
            /// </summary>
            public Entity Retrieve(IOrganizationService service, Entity en)
            {
                return service.Retrieve(en.LogicalName, en.Id, new ColumnSet("new_int", "new_string"));
            }

            /// <summary>
            /// 删除
            /// </summary>
            public void Delete(IOrganizationService service, Entity en)
            {
                service.Delete(en.LogicalName, en.Id);
            }

            /// <summary>
            /// 批量删除
            /// </summary>
            public void Delete(IOrganizationService service,EntityCollection ec)
            {
                if (ec != null && ec.Entities.Count > 0)
                {
                   foreach(Entity en in ec.Entities)
                   {
                       service.Delete(en.LogicalName, en.Id);
                   }
                }
            }

            /// <summary>
            /// 更新int类型的字段
            /// </summary>
            public void UpdateInt(IOrganizationService service, Entity en)
            {
                Entity updateEn = new Entity() { LogicalName = en.LogicalName,Id = en.Id };
                updateEn["new_int"] = 10;
                service.Update(updateEn);
            }

            /// <summary>
            /// 更新string类型的字段
            /// </summary>
            public void UpdateInt(IOrganizationService service, Entity en)
            {
                Entity updateEn = new Entity() { LogicalName = en.LogicalName, Id = en.Id };
                updateEn["new_string"] = "abc";
                service.Update(updateEn);
            }

            /// <summary>
            /// 更新float类型的字段
            /// </summary>
            public void UpdateFloat(IOrganizationService service, Entity en)
            {
                Entity updateEn = new Entity() { LogicalName = en.LogicalName, Id = en.Id };
                updateEn["new_float"] = 12.9;
                service.Update(updateEn);
            }

            /// <summary>
            /// 更新Money类型的字段
            /// </summary>
            public void UpdateMoney(IOrganizationService service, Entity en)
            {
                Entity updateEn = new Entity() { LogicalName = en.LogicalName, Id = en.Id };
                updateEn["new_money"] = new Money(12);
                service.Update(updateEn);
            }

            /// <summary>
            /// 更新OptionSetValue类型的字段
            /// </summary>
            public void UpdateOptionSetValue(IOrganizationService service, Entity en)
            {
                Entity updateEn = new Entity() { LogicalName = en.LogicalName, Id = en.Id };
                updateEn["new_optionsetvalue"] = new OptionSetValue(10);
                service.Update(updateEn);
            }

            /// <summary>
            /// 更新EntityReference类型的字段
            /// </summary>
            public void UpdateEntityReference(IOrganizationService service, Entity en)
            {
                Entity updateEn = new Entity() { LogicalName = en.LogicalName, Id = en.Id };
                updateEn["new_account"] = new EntityReference() { LogicalName = "account",Id = Guid.NewGuid() };
                service.Update(updateEn);
            }
        }
    }

     

     

    这是改进后的模式:

     

    /* 创建者:菜刀居士的博客
     * 创建日期:2014年07月06号
     */

    namespace Net.CRM.OrganizationService
    {
        using System;
        using Microsoft.Xrm.Sdk;
        using Microsoft.Xrm.Sdk.Query;

        /// <summary>
        /// 高速开发---OrganizationService
        /// </summary>
        public class OrganizationServiceQuickDemo
        {
            /// <summary>
            /// 查询
            /// </summary>
            public Entity Retrieve(IOrganizationService service, Entity en)
            {
                return service.Retrieve(en,"new_int", "new_string");
            }

            /// <summary>
            /// 删除
            /// </summary>
            public void Delete(IOrganizationService service, Entity en)
            {;
                 service.Delete(en);
            }

            /// <summary>
            /// 批量删除
            /// </summary>
            public void Delete(IOrganizationService service, EntityCollection ec)
            {
                service.Delete(ec);
            }

            /// <summary>
            /// 更新int类型的字段
            /// </summary>
            public void UpdateInt(IOrganizationService service, Entity en)
            {
                service.Update(en, "new_int", 10);
            }

            /// <summary>
            /// 更新string类型的字段
            /// </summary>
            public void UpdateInt(IOrganizationService service, Entity en)
            {
                service.Update(en, "new_string", "abc");
            }

            /// <summary>
            /// 更新float类型的字段
            /// </summary>
            public void UpdateFloat(IOrganizationService service, Entity en)
            {
                service.Update(en, "new_float", 12.9); 
            }

            /// <summary>
            /// 更新Money类型的字段
            /// </summary>
            public void UpdateMoney(IOrganizationService service, Entity en)
            {
                service.Update(en, "new_money", new Money(12)); 
            }

            /// <summary>
            /// 更新OptionSetValue类型的字段
            /// </summary>
            public void UpdateOptionSetValue(IOrganizationService service, Entity en)
            {
                service.Update(en, "new_optionsetvalue", new OptionSetValue(10));
            }

            /// <summary>
            /// 更新EntityReference类型的字段
            /// </summary>
            public void UpdateEntityReference(IOrganizationService service, Entity en)
            {
                service.Update(en, "new_account", new EntityReference() { LogicalName = "account", Id = Guid.NewGuid() });
            }
        }

         /// <summary>
        /// 扩展方法
        /// </summary>
        public static class EntityFunction
        {
            public static Entity Retrieve(this IOrganizationService service, Entity en,params string[] attrs)
            {
                return service.Retrieve(en.LogicalName, en.Id, new ColumnSet(attrs));
            }

            public static void Delete(this IOrganizationService service, Entity en)
            {
                service.Delete(en.LogicalName, en.Id);
            }

            public static void Delete(this IOrganizationService service, EntityCollection ec)
            {
                if (ec != null && ec.Entities.Count > 0)
                {
                    foreach (Entity en in ec.Entities)
                    {
                        service.Delete(en.LogicalName, en.Id);
                    }
                }
            }

            public static void Update<T>(this IOrganizationService service, Entity en, string name, T value)
            {
                Entity updateEn = new Entity() { LogicalName = en.LogicalName,Id = en.Id };
                updateEn[name] = value;
                service.Update(updateEn);
            }
        }
    }

    是不是开发快非常多。

     

    一般的,假设你的项目不是非常大,时间足够充分。这个时候就没有必要用高速开发了

    当你的项目非常大或者功能非常多,时间非常紧,这个时候高速开发就非常有必要了。

     

     

  • 相关阅读:
    关于凸函数的一个等价形式的应用
    获得url?后的参数
    文字转语音SpeechSynthesisUtterance
    bat批处理文件夹内文件名的提取【转载-改编】
    易经:当你感到不顺时,不要着急,3个小锦囊,助你尽快走出困境
    君子慎独,高人慎众!
    心乱,一切皆乱;心稳,才是根本!
    慎独,是最高级的独处!
    老实人必撞这三道南墙,看完趁早回头!
    与人交谈时,多说这几种话,情商越来越高!
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6886012.html
Copyright © 2020-2023  润新知