Dynamics CRM 2011 编程系列(25):插件的依赖注入
来源:互联网 发布:png软件图标 编辑:程序博客网 时间:2024/06/16 01:55
依赖注入在编程中已经不是什么新概念了,目前也有很多优秀的依赖注入框架可以供我们使用。如:nhibernate和hibernate。它们的实现原理就是将容易发生变化的东西写在配置文件中,然后制作一个解析程序来解析该配置文件并做相应的操作。
虽然它们非常优秀,但却不能直接使用在Dynamics CRM的开发中。直接用SQL对系统的数据表进行CRUD不是个明智的选择,既然这样我们就自己来简单的实现一个依赖注入程序吧。
具体操作如下:
图1 配置文件
图2 部署配置文件
图3 注册插件
图4
图5
图6
图7
插件代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Configuration;using System.Web.Configuration;using System.IO;using System.Xml;using Microsoft.Xrm.Sdk;using Microsoft.Xrm.Sdk.Messages;using Microsoft.Xrm.Sdk.Query;namespace Plugin23{ public class DependInjection:IPlugin { private string configFilePath = string.Empty; public DependInjection(string data) { configFilePath = data; } public void Execute(IServiceProvider serviceProvider) { IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = factory.CreateOrganizationService(null); Entity curEntity=(Entity)context.InputParameters["Target"]; XmlDocument doc = new XmlDocument(); doc.Load(configFilePath); XmlNode curPrcNode = doc.SelectSingleNode(string.Format("//Entity[@name='{0}']",curEntity.LogicalName.ToLower())); if (curPrcNode != null) { XmlNode curPrcStepNode = curPrcNode.SelectSingleNode(string.Format("//{0}", context.MessageName.ToLower())); if (curPrcStepNode != null) { foreach (XmlNode node in curPrcStepNode.ChildNodes) { if (node.Name.ToLower() == "createentity") { Entity tmpCrtEntity = new Entity(); var entityName = node.Attributes["name"].Value; tmpCrtEntity.LogicalName = entityName; foreach (XmlNode attr in node.ChildNodes) { tmpCrtEntity.Attributes.Add(attr.Attributes["name"].Value, attr.Attributes["value"].Value); } service.Create(tmpCrtEntity); } }//foreach }//if }//if } }}
配置文件
<?xml version="1.0" encoding="utf-8" ?><Entities> <Entity name="account"> <create> <createentity name="contact"> <Attribute name="lastname" value="Bank"></Attribute> </createentity> </create> <update></update> <delete></delete> <select></select> </Entity></Entities>
代码分析
配置文件中的节点“Entity”表示触发插件的实体,它的子节点“create”,“update”,“delete”,“select”分别代码插件中的create,update,delete,retrieve事件。节点“createentity”表示创建记录操作,而它的子点的“attribute”表示该创建的记录包含的属性值。
小结
上面只是一个简单的例子,如果要完全实现插件的依赖注入还需做很多的工作。如果在工作中需要大量使用插件的话,将插件实现依赖注入将会帮你省去不少时间。
- Dynamics CRM 2011 编程系列(25):插件的依赖注入
- Dynamics CRM 2011 编程系列(16):插件
- Dynamics CRM 2011编程系列
- Dynamics CRM 2011编程系列
- Dynamics CRM 2011 编程系列(18):插件中的Read操作
- Dynamics CRM 2011 编程系列(19):插件中的事务
- Dynamics CRM 2011 编程系列(20):插件队列
- Dynamics CRM 2011 编程系列(21):插件调试
- Dynamics CRM 2011 编程系列(23):用插件调用WCF
- Dynamics CRM 2011 编程系列(17):用插件来实现实体的CRUD
- Dynamics CRM 2011编程系列(43):JavaScript 的调试
- Dynamics CRM 2011 编程系列:自定义工作流
- Dynamics CRM 2011 编程系列(24):在插件中使用早期绑定
- Dynamics CRM 2011 编程系列(22):用插件调用Web Service
- Dynamics CRM 2011编程系列(55):Dynamics CRM 集成开发简述
- Dynamics CRM 2011 编程系列 (6):JS脚本编程之实体的CRUD[Create,Read]
- Dynamics CRM 2011 编程系列(7):JS编程之实体的CRUD[Update,Delete]
- Dynamics CRM 2011 编程系列(14):JS编程之表单控件的执行上下文
- CKEditor和CKFinder 解决图片上传
- 获取网页中播放的音乐文件
- Jquery的 scroll事件,实现当滚动条到达最底部时,自动加载新项
- Dynamics CRM 2011 编程系列(21):插件调试
- 使用lua虚拟机的一个注意事项
- Dynamics CRM 2011 编程系列(25):插件的依赖注入
- 每日一练
- php安装程序的版本选择
- 《Effective C++》条款03:两个成员函数如果只是常量性不同可以被重载
- Dynamics CRM 2011 编程系列(22):用插件调用Web Service
- TTS Events Explanation
- Dynamics CRM 2011 编程系列(23):用插件调用WCF
- Short URL implement
- 日期格式参考extjs api文档中的Date类型