首页 > 代码库 > crm快速开发之OrganizationService

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);
        }
    }
}

是不是开发快很多。

 

一般的,如果你的项目不是很大,时间足够充分。这个时候就没有必要用快速开发了

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