In this article I am trying to show how to create a plugin enabled application using .Net Reflection. The plugin enabled application loads an associated piece of software during the runtime. There should not be any design time binding for the specified component.
Advantages of Plugins
We can see lots of plugin enabled applications for example Winamp. The plugin enable application provides the following flexibilities:
- Reduced Size in Initial Deployment
- Incrementing the modules as plugins
- Customers can be benefited by choosing plugin modules thus reduction in cost
How Plugins works?
There will be 3 components in a plugin architecture application.
- Plugin container
- Plugin interface
- Plugins
The plugin container will serve as the host holding all plugins found in runtime.
The plugin interface provides as a standard of communication between the container and plugins.
The plugins are the actual piece of software which are located and loaded by the plugin container.
Execution
The plugin enabled application executes as depicted in the image below. After the application is executed, it will search for the plugins of associated interfaces. If any plugins are found they are instantiated and loaded into the application.
Diving into the Code
I hope we have had enough of theories. Now let us focus on how to build the actual code. In our example, we are trying to load plugins which contains forms. There is one Plugin.Core application which will perform as the plugin container and n number of plugin dlls which contain the forms.
Each form found will have a display name so that we can show it in the toolbar.
Component 1: Plugin Container .exe
This is a Windows Forms Application containing the main form to contain the plugins.
Component 2: Plugin. Interface.dll
This would be a Class Library containing the IPlugin interface.
public interface IPlugin
{
string Text { get; }
Form Form { get; }
Color BackColor { get; }
}
There will be a PluginLoader class which basically takes care of finding the plugins in a given folder and loading them into the system.
Component 3: BlueForm.Plugin.dll
This will be a Class Library project containing the plugins. The plugins are nothing other than classes which implements the IPlugin interface.
In our project, we are creating a plugin class named BlueFormPlugin which holds a form.
Note
For the time being I have used Windows Forms for a plugin example, but we can extend the same idea to ASP.NET, WPF, Windows Mobile applications too.
Summary
In this article the basic components of building the Plugin based application is explained. In the next part we can see the inner details of how the plugin can be identified and loaded into the application using Reflection.
Next Part
You can view the next part here.