一 订立契约
契约是应用程序和插件之间进行交互的依据和凭证。应用程序必须声明我有什么样的功能可被插件使用,并且插件必须符合什么条件才能被我使用。反之,插件必须要知道应用程序提供什么样的功能,我才能将自己的功能融入到应用程序的体系中。本系列文章主要讲如何使用.NET实现插件式的应用程序框架,所以其它的方式我就不再提了。
如何使用.NET订立契约呢?首先想到的Interface,其次是抽象类,但是在插件模式中我使用接口,因为我们是在满足应用程序的主要目的的基础上来提供附加的插件功能,就这一点来说,接口更灵活,更容易扩展。接下来,如何订立契约的内容呢?这就要根据你的业务需求了,为了讲解的方便,我们定义一个最最基本的插件式应用程序的插件契约。我们做一个假定,我们的应用程序是一个多文档的应用程序,包含一个主菜单栏,一个工具栏,菜单栏可以在程序的上下左右四个方向停靠,另外还有一个状态栏。到后边,如果有必要,我会扩展这个应用程序,让他本身提供更多的可供插件使用的功能。所以就目前而言,我想实现的功能就是让插件为主程序添加工具条,菜单项并实现一些简单的功能。
应用程序向插件提供服务有两种方式,一种是直接再应用程序接口中声明属性或者方法,一种是将应用程序接口声明成一个服务容器。我打算两种方式都用,明确的功能就在接口中直接声明成属性或者方法,另外将应用程序声明成一个服务容器,以方便插入更多的服务功能,提高应用程序的可扩展性。
下边是一个非常简单的应用程序接口定义,对于我们的假定已经足够了。
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Design;
using System.Windows.Forms;
namespace PluginFramework
{
public interface IApplication:IServiceContainer
{
ToolStripPanel LeftToolPanel { get;}
ToolStripPanel RightToolPanel { get;}
ToolStripPanel TopToolPanel { get;}
ToolStripPanel BottomToolPanel { get;}
MenuStrip MainMenuStrip { get;}
StatusStrip StatusBar { get;}
}
}
插件的接口定义:
using System.Collections.Generic;
using System.Text;
namespace PluginFramework
{
public interface IPlugin
{
IApplication Application { get;set;}
String Name { get;set;}
String Description { get;set;}
void Load();
void UnLoad();
event EventHandler<EventArgs> Loading;
}
}
我把完整源代码也附上,方便大家使用:源代码下载
二 动态加载
不管你采用什么方式实现插件式的应用程序框架,核心还是动态加载,换句话说,没有动态加载技术也就无所谓插件式的应用程序框架了。使用Com实现的话,你可以利用Com的API通过ProgID来动态创建COM对象,如果使用普通DLL,你需要使用Windows 的API函数LoadLibrary来动态加载DLL,并用GetProcAddress函数来获取函数的地址。而使用.NET技术的话,你需要使用Assembly类的几个静态的Load(Load,LoadFile,LoadFrom)方法来动态加载汇集。
一个Assembly里可以包含多个类型,由此可知,一个Assembly里也可以包含多个插件,就像前一篇文章所讲,只要它从IPlugin接口派生出来的类型,我们就承认它是一个插件类型。那么Assembly被动态加载了以后,我们如何获取Assembly里包含的插件实例呢?这就要用到反射(Reflection)机制了。我们需要使用Assembly的GetTypes静态方法来得到Assembly里所包含的所有的类型,然后遍历所有的类型并判断每一个类型是不是从IPlugin接口派生出来的,如果是,我们就使用Activator的静态方法CreateInstance方法来获得这个插件的实例。.NET的动态加载就是这几个步骤。下来,我做一个简单的例子来演练一下动态加载。首先声明一点,这个例子非常简单,纯粹是为了演练动态加载,我们的真正的插件式的应用程序框架里会有专门的PluginService来负责插件的加载,卸载。
我们的插件位于一个DLL里,所以我们首先创建一个Class library工程。创建一个FirstPlugin类让它派生于IPlugin接口,并实现接口的方法和属性,由于本文的目的是演示动态加载,所以IPlugin接口的Loading事件我们就不提供默认的实现了,虽然编译的时候会给出一个警告,我们不必理会它。这个插件的功能就是在应用程序里创建一个停靠在主窗体底部的ToolStrip,这个ToolStrip上有一个按钮,点击这个按钮,会弹出一个MessageBox显示“The first plugin”。下面是代码:
using System.Collections.Generic;
using System.Text;
using PluginFramework;
using System.Windows.Forms;
namespace FirstPlugin
{
public class FirstPlugin:IPlugin
{
private IApplication application = null;
private String name="";
private String description = "";
#region IPlugin Members
public IApplication Application
{
get
{
return application;
}
set
{
application = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Description
{
get
{
return description;
}
set
{
description = value;
}
}
public void Load()
{
if (application != null && application.BottomToolPanel != null)
{
//创建一个向主程序添加的ToolStrip
ToolStrip sampleToolStrip = new ToolStrip();
ToolStripButton button = new ToolStripButton("Click Me");
button.Click += new EventHandler(button_Click);
sampleToolStrip.Items.Add(button);
//在主程序的底端添加ToolStrip
application.BottomToolPanel.Controls.Add(sampleToolStrip);
}
}
void button_Click(object sender, EventArgs e)
{
MessageBox.Show("The first plugin");
}
//相关的文章主要讲动态加载,所以卸载就不实现了
public void UnLoad()
{
throw new Exception("The method or operation is not implemented.");
}
public event EventHandler<EventArgs> Loading;
#endregion
}
}
接下来我们创建一个Windows Application工程让主窗体派生于IApplication接口并实现IApplication接口的方法和属性,下来我们声明1个MenuStrip和1个StatusStrip,让他们分别停靠在窗口的顶部和底端,接下来我们声明4个ToolStripPanel,分别人他们停靠在上下左右四个边,最后我们创建一个ToolStrip,在上边添加一个按钮,当点击这个按钮的时候,我们动态的加载插件。
为了方便演示,我们把生成的Assembly放置到固定的位置,以方便主程序加载,在本例里,我们在应用程序所在的文件夹里创建一个子文件夹Plugins(E:/Practise/PluginSample/PluginSample/bin/Debug/Plugins),将插件工程产生的Assembly(FirstPlugin.dll)放置在这个子文件夹。下面是动态加载的代码:
{
//动态加载插件,为了方便起见,我直接给出插件所在的位置
String pluginFilePath = Path.GetDirectoryName(Application.ExecutablePath) + "//plugins//FirstPlugin.dll";
Assembly assembly = Assembly.LoadFile(pluginFilePath);
//得到Assembly中的所有类型
Type[] types = assembly.GetTypes();
//遍历所有的类型,找到插件类型,并创建插件实例并加载
foreach (Type type in types)
{
if (type.GetInterface("IPlugin") != null)//判断类型是否派生自IPlugin接口
{
IPlugin plugin = (IPlugin)Activator.CreateInstance(type);//创建插件实例
plugin.Application = this;
plugin.Load();
}
}
}
三 管理插件
我们现在已经搭建了插件式的应用程序框架,接下来的工作就是要充实框架的内容,提供基本的服务,也就是Service。我想首要的任务就是提供插件的管理服务,我在前面的文章也提到了,要实现动态加载必须要知道插件寄宿在哪里,哪些要加载,哪些不加载,这些就是这篇文章要讨论的问题。
首先解决的就是插件放在什么地方,我采取的传统的方法,将插件放到应用程序所在目录下的制定目录,我会在应用程序所在的目录下创建一个文件夹,命名为Plugins。接下来的工作就是要通知哪些插件是要加载的,哪些是不需要加载的,我会将这些信息放到应用程序的配置文件中的制定配置块中,当应用程序运行的时候,就会读取配置文件,并根据获得的信息加载插件。另外我们的应用程序框架是建立在Service基础之上,所以我需要创建一个管理插件的service。
我们现在定义一个插件管理的Service接口。
using System.Collections.Generic;
using System.Text;
namespace PluginFramework
{
public interface IPluginService
{
IApplication Application { get;set;}
void AddPlugin(String pluginName, String pluginType, String Assembly, String pluginDescription);
void RemovePlugin(String pluginName);
String[] GetAllPluginNames();
Boolean Contains(String pluginName);
Boolean LoadPlugin(String pluginName);
Boolean UnLoadPlugin(String pluginName);
IPlugin GetPluginInstance(String pluginName);
void LoadAllPlugin();
}
}
PluginService要实现的目标首先是在配置文件中添加/删除要加载的插件以及相关的信息,接下来就是动态的加载插件。我们要定义几个类型:Plugin配置区块类型,Plugin元素类型,plugin元素集合类型,以便我们能够读取插件的信息。
最后我们实现PluginService:
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Configuration;
using System.Reflection;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace PluginFramework
{
public class PluginService : IPluginService
{
private IApplication application = null;
private PluginConfigurationSection config = null;
private Dictionary<String, IPlugin> plugins = new Dictionary<string, IPlugin>();
private XmlDocument doc = new XmlDocument();
public PluginService()
{
}
public PluginService(IApplication application)
{
this.application = application;
}
#region IPluginService Members
public void AddPlugin(string pluginName, string pluginType, string assembly, string pluginDescription)
{
doc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
XmlNode pluginNode = doc.SelectSingleNode("/configuration/PluginSection");
XmlElement ele = doc.CreateElement("add");
XmlAttribute attr = doc.CreateAttribute("Name");
attr.Value = pluginName;
ele.SetAttributeNode(attr);
XmlAttribute attrType = doc.CreateAttribute("Type");
attrType.Value = pluginType;
ele.SetAttributeNode(attrType);
XmlAttribute attrAss = doc.CreateAttribute("Assembly");
attrAss.Value = assembly;
ele.SetAttributeNode(attrAss);
XmlAttribute attrDes = doc.CreateAttribute("Description");
attrDes.Value = pluginDescription;
ele.SetAttributeNode(attrDes);
pluginNode.AppendChild(ele);
doc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
ConfigurationManager.RefreshSection("PluginSection");
}
public void RemovePlugin(string pluginName)
{
doc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
XmlNode node = doc.SelectSingleNode("/configuration/PluginSection");
foreach (XmlNode n in node.ChildNodes)
{
if (n.Attributes != null)
{
if (n.Attributes[0].Value == pluginName)
{
node.RemoveChild(n);
}
}
}
doc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
ConfigurationManager.RefreshSection("PluginSection");
}
public string[] GetAllPluginNames()
{
config = (PluginConfigurationSection)ConfigurationManager.GetSection("PluginSection");
PluginConfigurationElement pe = new PluginConfigurationElement();
ArrayList ps = new ArrayList();
for (Int32 i = 0; i < config.PluginCollection.Count; i++)
{
pe = config.PluginCollection[i];
ps.Add(pe.Name);
}
return (String[])ps.ToArray(typeof(String));
}
public bool Contains(string pluginName)
{
config = (PluginConfigurationSection)ConfigurationManager.GetSection("PluginSection");
PluginConfigurationElement pe = new PluginConfigurationElement();
List<String> ps = new List<string>();
for (Int32 i = 0; i < config.PluginCollection.Count; i++)
{
pe = config.PluginCollection[i];
ps.Add(pe.Name);
}
return ps.Contains(pluginName);
}
public bool LoadPlugin(string pluginName)
{
Boolean result = false;
config = (PluginConfigurationSection)ConfigurationManager.GetSection("PluginSection");
PluginConfigurationElement pe = new PluginConfigurationElement();
String path = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "//Plugin";
try
{
for (Int32 i = 0; i < config.PluginCollection.Count; i++)
{
pe = config.PluginCollection[i];
if (pe.Name == pluginName)
{
Assembly assembly = Assembly.LoadFile(path + "//" + pe.Assembly);
Type type = assembly.GetType(pe.Type);
IPlugin instance = (IPlugin)Activator.CreateInstance(type);
instance.Application = application;
instance.Load();
plugins[pluginName] = instance;
result = true;
break;
}
}
if (!result)
{
MessageBox.Show("Not Found the Plugin");
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
result = false;
}
return result;
}
public bool UnLoadPlugin(string pluginName)
{
Boolean result = false;
try
{
IPlugin plugin = GetPluginInstance(pluginName);
plugin.UnLoad();
result = true;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return result;
}
public void LoadAllPlugin()
{
PluginConfigurationElement pe = new PluginConfigurationElement();
config = (PluginConfigurationSection)ConfigurationManager.GetSection("PluginSection");
String path = Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "//Plugin";
try
{
for (Int32 i = 0; i < config.PluginCollection.Count; i++)
{
pe = config.PluginCollection[i];
Assembly assembly = Assembly.LoadFile(path + "//" + pe.Assembly);
Type type = assembly.GetType(pe.Type);
IPlugin instance = (IPlugin)Activator.CreateInstance(type);
instance.Application = application;
instance.Load();
plugins[pe.Name] = instance;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
public IApplication Application
{
get
{
return application;
}
set
{
application = value;
}
}
public IPlugin GetPluginInstance(string pluginName)
{
IPlugin plugin = null;
if (plugins.ContainsKey(pluginName))
{
plugin = plugins[pluginName];
}
return plugin;
}
#endregion
}
}
由于代码比较多,我也就不一一列举了,只把比较重要的代码列出来,其余的我会提供源代码的下载。在实现了PluginService以后,我们需要有一个地方能够使用这个Service来管理插件,我的做法是在一个菜单里添加一个项目,当用户点击这个项目的时候弹出插件管理的对话框,用户在这个对话框中选择使用那些插件,当插件被选中的时候,插件会被立即加载进来,并且记录到配置文件里,当用户下次运行应用程序的时候,插件默认会被自动的加载。
另外从现在开始我们就需要使用配置文件了,所以,我们需要给应用程序添加一个app.config文件,文件内容如下:
< configuration >
< configSections >
< section
name ="PluginSection"
type ="PluginFramework.PluginConfigurationSection, PluginFramework"
/>
</ configSections >
< PluginSection >
</ PluginSection >
</ configuration >
样子,总体来说我们就为Plugin的管理提供了一个基本的实现,如果大家还有什么不明白的地方,可以参考我提供的源代码或者通过e-mail和我联系。
源代码下载