I’ve been working with a Plugin model for a few weeks now and I thought it was time I should document the model.
Task is to build an application that you can plugin models via the .config file, and provide a level of configuration for each plugin via the .config file.
This example is for the purposes of example and you should perform more checks to perform validation to ensure that the application is more rebust. Oh, and you can now do all of this using IoC containers, which I may cover another day. But I have found serious issues with IoC containers and VB.NET, as VB.NET does not support implicit interface implementation whereas C# does, but that is another issue for another day.
First lets create the interfaces that will be required for a Plugin to work
The PlugInName and Version are just so we can hold general information about the Plugins.
The Initialise method will be called to start the plugin up and when you are finished you must call the Finalise method.
I have included InstalledPlugInElement, which will hold all the configuration information for the plugin that can be stored in the .config file.
Lets have a quick look at the InstalledPlugInElement class
Here are hold the assembly name and a configuration parameter for to enable the Cache for the plugin
Next we need to handle the .config file and the section where the plugs are to be configured, this will be held in the InstalledPlugInSectionHandler, this inherits the ConfigurationSection, as seen below
Okay that’s it for now with what we need for the plugins, next we need to be able to load the plugins, with the help of a PlugInLoader
Okay this is quite a lot of code for one method.
What we are going to do in this method is get a list of plugins that are supplied in the .config file, and when we create a new instance of the plugin we’ll pass in the Installed plugin Elements that are in the .config file.
The section of the .config that holds the plugin information is “plug_ins”, and we get a list of said plugins from the .config file.
You will also require InstalledPlugInCollection, which you’ll find in the source at the bottom of this article.
Of now lets create our PlugIn, generate a new project and add a controller class
Of course this controller class implments the IPlugIn interface we created earlier.
We set the Plugin Name and Version number.
What you will also see is we can set the Cache Enabled, which is pulled in from the .config file
Now we can load the plugins in to a collection of plugins
That’s it for plugins, in another article I will go over how the determine if a plugin implements an interface that you may wish to use.
PlugInExample.zip (42.28 kb)