Dynamics CRM 2011 编程系列(25):插件的依赖注入
2012-06-01 14:30
531 查看
依赖注入在编程中已经不是什么新概念了,目前也有很多优秀的依赖注入框架可以供我们使用。如:nhibernate和hibernate。它们的实现原理就是将容易发生变化的东西写在配置文件中,然后***一个解析程序来解析该配置文件并做相应的操作。
虽然它们非常优秀,但却不能直接使用在Dynamics CRM的开发中。直接用SQL对系统的数据表进行CRUD不是个明智的选择,既然这样我们就自己来简单的实现一个依赖注入程序吧。
具体操作如下:
图1 配置文件
图2 部署配置文件
图3 注册插件
图4
图5
图6
图7
虽然它们非常优秀,但却不能直接使用在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 编程系列(19):插件中的事务
- Dynamics CRM 2011 编程系列(21):插件调试
- Dynamics CRM 2011 编程系列(22):用插件调用Web Service
- Dynamics CRM 2011 编程系列(16):插件
- Dynamics CRM 2011 编程系列(23):用插件调用WCF
- Dynamics CRM 2011 编程系列(17):用插件来实现实体的CRUD
- Dynamics CRM 2011 编程系列(18):插件中的Read操作
- Dynamics CRM 2011 编程系列(24):在插件中使用早期绑定
- Dynamics CRM 2011 编程系列(20):插件队列
- Dynamics CRM 2011 编程系列(13):JS编程之Who Am I
- Dynamics CRM 2011 编程系列 (6):JS脚本编程之实体的CRUD[Create,Read]
- Dynamics CRM 2011 编程系列(9):JS编程之客户端实体
- Dynamics CRM 2011 编程系列:自定义工作流
- Dynamics CRM 2011 编程系列(2):JS脚本编程之onload事件
- Dynamics CRM 2011 编程系列(5):JS脚本编程之实战一
- Dynamics CRM 2011 编程系列(32):Ribbon的开发二(按钮的禁用规则)
- Dynamics CRM 2011 编程系列(33):Ribbon的开发三(按钮的执行环境)
- Dynamics CRM 2011 编程系列(10):JS编程之构造OData查询字符串
- Dynamics CRM 2011 编程系列(28):使用SQL Server Profiler找表
- 关于Dynamics CRM 2011 编程系列的写作建议(邮箱: ghost_bear@163.com)