Build a plugin system for IBuySpy

Source: Internet
Author: User
Tags foreach config httpcontext modify
In the words of December 17, 2003, the MSDN Library website has a silent article on the basics of building a plugin framework, so, with this essay ...

PlugIn, very cool feature, here's how to customize a page start PlugIn for our IBuySpy, this PlugIn lets the user create the PlugIn themselves, embed it in the IBuySpy page start PlugIn, When the site page is loaded, the user's embedded plugin is executed.

But why do we have to create plugin interfaces for IBuySpy? We want to achieve the same function, can directly modify its code, is not directly a lot? Reason: IBuySpy is only used to demonstrate plugin, you can apply the same technology to other webform or even WinForm, and they may not be as free as IBuySpy, we deliver the product without code, If we provide a plugin interface, it will undoubtedly make our products more scalable. Besides, many functions may be directly made into plugin to embed into the original system, this time no longer need to change the original code, and then compile it.

1. Build the interfaces required by the IBuySpy plugin, which can be exposed to user code:

The first thing you need is a generic IPlugin interface, and all the specific plug-ins will implement this interface:

Namespace Aspnetportal.plugins {

Public interface IPlugIn {
String Name {get;}
String Version {get;}
void Doaction (Ipluginargs args);
}
}

This interface has three members:
Name property, exposing the names of Plug-ins
Version properties, exposing the versions of Plug-ins
Doaction () method that performs the operation that the plug-in will do, and this method also has a Ipluginargs parameter that needs to be passed to the method parameter that can be passed through it.

And then there's this Ipluginargs interface:

Namespace Aspnetportal.plugins {

Public interface Ipluginargs {
System.Web.HttpContext context {get;}
Object Data {get;}
}
}

It has two members:
Context Properties, a HttpContext type of object, if we want to allow plug-ins to do something on the page, do not give it HttpContext certainly not.
The Data property, an object of type Object, reserved, where it needs to be used.

Next is an interface collection class:

Namespace Aspnetportal.plugins {

public class Plugincollection:collectionbase {

Public Int32 Add (IPlugIn plugIn) {
return this. List.add (PlugIn);
}

Public IPlugIn This[int32 Index] {
get {
Return (IPlugIn) this. List[index];
}
}
}
}

It's simple and straightforward. Users may not be embedding more than one plug-in.

PlugIn can also have a number of types, such as the page Start PlugIn we're implementing here, which allows embedded PlugIn to execute when the page is loaded. Of course you can also create various types of plugin.

Namespace Aspnetportal.plugins {

Public interface Ipagestartplugin:iplugin {}
}

This plugin interface does not need to do anything, direct inheritance iplugin can be.

2, modify IBuySpy, let it support the implementation of plugin:

According to our requirements, we create an actual plug-in parameter class that inherits from the Ipluginargs:

Namespace Aspnetportal.plugins {

public class Pluginargs:ipluginargs {
Private System.Web.HttpContext _context;
Private Object _data;

Public Pluginargs (System.Web.HttpContext context, Object data) {
_context = context;
_data = data;
}

Public System.Web.HttpContext Context {
get {
return _context;
}
}

Public Object Data {
get {
return _data;
}
}
}
}

We need a place to mark the user's embedded list of PlugIn, we put it inside the web.config, and we add an item to indicate the page Start PlugIn we want to add:

"ADD key=" Pagestartplugins "value=" "/"

Value can be written into the embedded plugin list, formatted like this: value = "Plug-in one's class name, plug-in one's assembly name;" Plug-in two's class name, plug-in Two's assembly name "

Then we build a Pluginhelper class to perform the operations of getting plugin, performing plugin:

Namespace Aspnetportal.plugins {

public class Pluginhelper {

Private Pluginhelper () {}

public static plugincollection Getplugins (String plugintype) {
Plugincollection PlugIns = new Plugincollection ();
String spagestartplugins = System.configuration.configurationsettings.appsettings[plugintype];
if ((Spagestartplugins!= null) && (spagestartplugins!= "")) {
string[] Aspluginstr = Spagestartplugins.split (';'); foreach (String pluginstr in aspluginstr) {
Plugins.add ((Ipagestartplugin) System.Activator.CreateInstance (System.Type.GetType (PLUGINSTR)));
}
}
return plugIns;
}

public static void Executeplugins (Plugincollection plugIns, Ipluginargs args) {
foreach (IPlugIn plugIn in PlugIns) {
Plugin.doaction (args);
}
}
}
}

The Getplugins () method returns a list of plugin of the specified type, which is plugincollection,executeplugins () to perform the plugin in the parameter.

Finally, we want to add the code that executes the plug-in to the execution queue of the page. To be able to perform user-embedded page start Plug at the beginning of each page, The standard approach is to implement it in Global.asa, and the more standard approach is to build a HttpModule, and then execute the plugin in this httpmodule (about building a custom HttpModule, the Chili Peppers are a master). I'm lazy here because IBuySpy almost all content pages are desktopdefault.aspx this page, so let's start by creating a method in this page to execute the plugin:

private void Performplugins () {
Plugincollection PlugIns = Pluginhelper.getplugins ("Pagestartplugins");
Pluginargs args = new Pluginargs (context, NULL);
Pluginhelper.executeplugins (PlugIns, args);
}

The first sentence gets all the page Start PlugIn, the second sentence creates a Pluginargs object that is passed to the plug-in, and the third calls Pluginhelper.executeplugins () to execute the first sentence of the PlugIn queue.

It is OK to call the Performplugins () method above in the Desktopdefault.aspx Page_Init event.

3. Demonstrates how to create a page Start PlugIn:

The previous two steps have allowed IBuySpy to embed the user-defined page Start plugin, and now we're going to do an actual plugin embedded in it.

Start VS, Create a "C # Class Library" project, The introduction of IBuySpy Assembly Portal.dll is because we need to use the plugin-related interfaces in IBuySpy, and a better way is to put the IBuySpy-related public interface in a separate assembly.

Namespace Welcomemessage {

public class ShowWelcome:ASPNetPortal.PlugIns.IPageStartPlugIn {

public string Name {
get {
Return "show Page Welcome message";
}
}

public string Version {
get {
return "1.0.0.1";
}
}

public void Doaction (Ipluginargs args) {
Args. Context.Response.Write ("<_script_>alert (' Hello, world! ');"); Deliberately write wrong, otherwise ...
}
}
}

This Showwelcome class inherits from Ipagestartplugin, which means that it is a page Start PlugIn, the Doaction () method that is used to perform the action only does one thing and gets the page-related HttpContext object from the argument. Then output a section of the word specifier a "hello,world!" before the page content is loaded The Prompt box.

Then we modify IBuySpy's web.config inside the relevant sentence setting:

"ADD value=" Welcomemessage.showwelcome, Welcomemessage "key=" Pagestartplugins "/"

OK, compile, put the generated DLL into the IBuySpy Bin directory, open the browser IBuySpy Web site, you will see every time you load the page content, will pop up a "hello,world!" The Prompt box.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.