• Dynamics Extentions BasePlugin


    Dynamics 365/CRM 插件基础扩展,插件继承此类后,可以简化一些基础服务的初始化以及Target等参数的取用;

    以下为基本使用方式,更多能力请自动探索

    1. using lce.mscrm.engine;
    2. using Microsoft.Xrm.Sdk;
    3. namespace MSCRM.CRM.Plugins.Fund
    4. {
    5. ///
    6. /// 测试实体Demo展示
    7. /// 测试实体 lce_test
    8. /// Entity:lce_test,Message:Create,Event:Pre
    9. /// Entity:lce_test,Message:Create\Update,Event:Post
    10. ///
    11. public class TestPost : BasePlugin
    12. {
    13. public override void HandleExecute(IOrganizationService service, IOrganizationService caller, ITracingService tracing, IPluginExecutionContext context)
    14. {
    15. if (EqualActionOrMessage(20, MessageName.Create) && Target.IsNotNull("lce_name"))
    16. {
    17. HandleCreateLoigc(service, Target);
    18. }
    19. if (EqualActionOrMessage(40, MessageName.Create) && Target.IsNotNull("lce_name"))
    20. {
    21. HandleCreateLoigc(service, Target);
    22. }
    23. if (EqualActionOrMessage(40, MessageName.Update) && Target.IsNotNull("lce_name"))
    24. {
    25. HandleUpdateLoigc(service, Target);
    26. }
    27. }
    28. private void HandleCreateLoigc(IOrganizationService service, Entity target)
    29. {
    30. //do things
    31. }
    32. private void HandleUpdateLoigc(IOrganizationService service, Entity target)
    33. {
    34. //do things
    35. }
    36. }
    37. }

    以下为BasePlugin扩展类源代码

    1. /* file name:lce.mscrm.engine.BasePlugin.cs
    2. * author:lynx lynx.kor@163.com @ 2018/10/28 13:13:00
    3. * copyright (c) 2020 Copyright@lynxce.com
    4. * desc:
    5. * > add description for BasePlugin
    6. * revision:
    7. *
    8. */
    9. using lce.ext.providers;
    10. using Microsoft.Xrm.Sdk;
    11. using System;
    12. using System.ServiceModel;
    13. namespace lce.mscrm.engine
    14. {
    15. #region === MessageName ===
    16. public enum MessageName
    17. {
    18. ///
    19. /// Assign
    20. /// Changes ownership of a record. Valid for user-owned or team-owned entities.
    21. ///
    22. Assign,
    23. ///
    24. /// Creates links between a record and a collection of records where there is a relationship
    25. /// between the entities.
    26. ///
    27. Associate,
    28. ///
    29. /// Create Target
    30. /// Creates a record of a specific entity type, including custom entities.
    31. ///
    32. Create,
    33. ///
    34. /// Delete Target
    35. /// Deletes a record.
    36. ///
    37. Delete,
    38. ///
    39. /// Removes links between a record and a collection of records where there is a relationship
    40. /// between the entities.
    41. ///
    42. Disassociate,
    43. AssignUserRoles,
    44. GrantAccess,
    45. ModifyAccess,
    46. ///
    47. /// Retrieves a record.
    48. ///
    49. Retrieve,
    50. ///
    51. /// Retrieves a collection of records.
    52. ///
    53. RetrieveMultiple,
    54. RetrievePrincipalAccess,
    55. RetrieveSharedPrincipalsAndAccess,
    56. RevokeAccess,
    57. ///
    58. /// Set the state of a record.
    59. ///
    60. SetState,
    61. ///
    62. /// Grants, modifies or revokes access to a record to another user or team. Valid for
    63. /// user-owned or team-owned entities.
    64. ///
    65. Share,
    66. ///
    67. /// Update Target
    68. /// Modifies the contents of a record.
    69. ///
    70. Update,
    71. }
    72. #endregion === MessageName ===
    73. ///
    74. /// action:BasePlugin
    75. /// UserId = _context.UserId
    76. ///
    77. public abstract class BasePlugin : IPlugin
    78. {
    79. #region === 私有变量/方法 ===
    80. private IPluginExecutionContext _context;
    81. ///
    82. ///
    83. ///
    84. ///
    85. ///
    86. private T GetService<T>(IServiceProvider serviceProvider)
    87. {
    88. return (T)serviceProvider.GetService(typeof(T));
    89. }
    90. #endregion === 私有变量/方法 ===
    91. ///
    92. /// 修改后
    93. ///
    94. protected Entity PostImage { get; set; }
    95. ///
    96. /// 修改前
    97. ///
    98. protected Entity PreImage { get; set; }
    99. ///
    100. /// 当前实例
    101. ///
    102. protected Entity Target { get; set; }
    103. ///
    104. /// 当前实例
    105. ///
    106. protected EntityReference TargetReference { get; set; }
    107. ///
    108. /// check the attribute in entity is null.
    109. ///
    110. ///
    111. ///
    112. ///
    113. public static bool IsNotNull(Entity entity, string attribute)
    114. {
    115. return null != entity && entity.Contains(attribute) && IsNotNull(entity[attribute]);
    116. }
    117. ///
    118. /// check the object is not null.
    119. ///
    120. ///
    121. ///
    122. public static bool IsNotNull(object obj)
    123. {
    124. return null != obj && DBNull.Value != obj && !string.IsNullOrEmpty(obj.ToString());
    125. }
    126. ///
    127. /// 插件回滚,弹出消息。
    128. ///
    129. /// 消息类容
    130. public static void ShowErrorMessage(string msg)
    131. {
    132. throw new InvalidPluginExecutionException(msg);
    133. }
    134. ///
    135. /// 实体状态比较
    136. ///
    137. /// pre:20;post:40
    138. /// create,update,delete
    139. ///
    140. public bool EqualActionOrMessage(int state, MessageName action)
    141. {
    142. return _context.Stage == state && _context.MessageName.ToLower().Equals(action.ToString().ToLower());
    143. }
    144. ///
    145. ///
    146. ///
    147. public void Execute(IServiceProvider serviceProvider)
    148. {
    149. _context = GetService(serviceProvider); //上下文
    150. var _tracing = GetService(serviceProvider); //跟踪服务
    151. var _factory = GetService(serviceProvider); //服务工厂
    152. var _caller = _factory.CreateOrganizationService(_context.UserId); //用户权限服务
    153. var _service = _factory.CreateOrganizationService(null); //管理员权限服务
    154. try
    155. {
    156. if (_context.InputParameters.Contains("Target") && _context.InputParameters["Target"] is EntityReference)
    157. {
    158. TargetReference = (EntityReference)_context.InputParameters["Target"];
    159. }
    160. if (_context.InputParameters.Contains("Target") && _context.InputParameters["Target"] is Entity)
    161. {
    162. Target = (Entity)_context.InputParameters["Target"];
    163. TargetReference = Target.ToEntityReference();
    164. }
    165. if (_context.PreEntityImages.Contains("PreImage") && _context.PreEntityImages["PreImage"] is Entity)
    166. PreImage = _context.PreEntityImages["PreImage"];
    167. if (_context.PostEntityImages.Contains("PostImage") && _context.PostEntityImages["PostImage"] is Entity)
    168. PostImage = _context.PostEntityImages["PostImage"];
    169. // do same logic
    170. HandleExecute(_service, _caller, _tracing, _context);
    171. }
    172. catch (InvalidPluginExecutionException ex)
    173. {
    174. _tracing.Trace($"操作提醒:{ex.Message}[Plugin]");
    175. throw ex;
    176. }
    177. catch (FaultException ex)
    178. {
    179. var msg = $"操作错误:{ex.Message}";
    180. if (null != ex.InnerException)
    181. msg = $"{msg}=>{ex.InnerException.Message}";
    182. _tracing.Trace($"插件业务错误:{msg}{ex.StackTrace}");
    183. LogExt.e($"{_context.MessageName}:{TargetReference.LogicalName}:{TargetReference.Id}:{msg}", ex, $"plugin.{TargetReference.LogicalName}");
    184. throw new InvalidPluginExecutionException(msg);
    185. }
    186. catch (Exception ex)
    187. {
    188. var msg = $"系统错误:{ex.Message}";
    189. if (null != ex.InnerException)
    190. msg = $"{msg}=>{ex.InnerException.Message}";
    191. _tracing.Trace($"系统内部错误:{msg}{ex.StackTrace}");
    192. LogExt.e($"{_context.MessageName}:{TargetReference.LogicalName}:{TargetReference.Id}:{msg}", ex, $"plugin.{TargetReference.LogicalName}");
    193. throw new InvalidPluginExecutionException(msg);
    194. }
    195. }
    196. ///
    197. /// 插件继承BasePlugin后实现些方法,进行业务处理
    198. ///
    199. public abstract void HandleExecute(IOrganizationService service, IOrganizationService caller, ITracingService tracing, IPluginExecutionContext context);
    200. }
    201. }

  • 相关阅读:
    Java-API简析_java.util.Scanner类(基于 Latest JDK)(浅析源码)
    【沐风老师】3DMAX散布插件scat_pro v1.1使用教程
    three 模型对象、材质
    【算法与数据结构】46、47、LeetCode全排列I, II
    基于C#使用winform技术的游戏平台的实现【C#课程设计】
    redis 6.2.12集群部署
    c++八股day3-c++什么时候生成默认拷贝构造函数
    idea控制台背景色
    【Docker】docker安装nacos
    论文翻译:2020_DCCRN: Deep Complex Convolution Recurrent Network for Phase-Aware Speech Enhancement
  • 原文地址:https://blog.csdn.net/lynxkor/article/details/128170037