您的位置:首页 > 编程语言

Dynamics CRM 2011 编程系列(25):插件的依赖注入

2012-06-01 14:30 531 查看
依赖注入在编程中已经不是什么新概念了,目前也有很多优秀的依赖注入框架可以供我们使用。如: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”表示该创建的记录包含的属性值。



小结

上面只是一个简单的例子,如果要完全实现插件的依赖注入还需做很多的工作。如果在工作中需要大量使用插件的话,将插件实现依赖注入将会帮你省去不少时间。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: