Sometimes we came in situation when we need to design an architecture which supports plugin of components at runtime. That facilitates developer to not to compile the main application again and again for new addition of components/plugins and just with a little configuration it should be activated in the application. Therefore, this post shows the plugin framework which helps to plug in components with little configuration.
I took the logger example to develop this framework. In which I will show you how to plug in multiple types of logger into the application. The application is very simple just to show an idea which logs the text from the textbox control on the button click event. System checks the plugins that are available and call the ExecuteTask method of each plugin attached with the application.
Let’s start some coding work.
Create Plugin Framework
- Create a new class library project in .net to develop a plugin framework.
- Create an interface named IPlugin.cs
-
Define a property “Name” and method “ExecuteTask” which takes String message as a parameter.
namespace PluginFramework
{
public
interface
IPlugin
{
String name { get; }
void ExecuteTask(String message);
}
}
- Actually the concept behind this framework is that developer does not need to reference new plugins dll or class libary they will just add it in the application configuration file. We will provide an specific section for Plugins. Therefore, we need to create a class which implements IConfigurationSectionHandler and define CreateTask method.
- The code snippet for PluginSectionHandler as follows
namespace PluginFramework
{
public
class
PluginSectionHandler : IConfigurationSectionHandler
{
ArrayList plugins = new
ArrayList();
public
object Create(object parent, object configContext, System.Xml.XmlNode section)
{
foreach(XmlNode plugin in section.ChildNodes)
{
object plugObject= Activator.CreateInstance(Type.GetType(plugin.Attributes[“type”].Value));
plugins.Add(plugObject);
}
return plugins;
}
}
}
Create Custom Plugins
- Create another class library project named as “MessagePlugin”. This library contains all types of logger which will be added as a plugin In the main application.
- I will create two classes one which write the message in the event log and the other write it in the trace window.
- Therefore, create a new TraceMessage class and implement the IPlugin interface.
-
Define the body for ExecuteTask method as follows.
public
class
TraceMessage : IPlugin
{
public
string name
{
get { return
“Trace Logger”; }
}
public
void ExecuteTask(string message)
{
System.Diagnostics.Trace.WriteLine(message);
}
}
- For EventLogMessage plugin, create a new class and implement the IPlugin interface.
-
Define the body for ExecuteTask method as follows.
public
class
EventLogMessage : IPlugin
{
public
string name
{
get
{
return
“Event Logger”;
}
}
public
void ExecuteTask(String message)
{
if (!System.Diagnostics.EventLog.SourceExists(“PluginFramework”))
{
System.Diagnostics.EventLog.CreateEventSource(“PluginFramework”, “MyLog”);
}
System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
log.Source = “PluginFramework”;
log.WriteEntry(message);
}
}
Create Main Application
-
Create a Windows Application project and add two controls TextBox and a button on the form.
- The concept is that developer will not reference the MessagePlugin library created above in the windows application and he will just define the Type and assembly in the app.config file. When the application starts it will load all the sections added. To do this, we will add app.config file first.
-
Open the app.config file and place a configsections tag and under configsections tag define a new section tag.
<configSections>
<section
name=“plugins“
type=“PluginFramework.PluginSectionHandler, PluginFramework“/>
</configSections>
-
Now add the plugins in the configuration file as below.
<plugins>
<plugin
type=“MessagePlugin.EventLogMessage, MessagePlugin“ />
<plugin
type=“MessagePlugin.TraceMessage, MessagePlugin“ />
</plugins>
Complete app.config will look like as follows.
<?xml
version=“1.0“
encoding=“utf-8“ ?>
<configuration>
<configSections>
<section
name=“plugins“
type=“PluginFramework.PluginSectionHandler, PluginFramework“/>
</configSections>
<plugins>
<plugin
type=“MessagePlugin.EventLogMessage, MessagePlugin“ />
<plugin
type=“MessagePlugin.TraceMessage, MessagePlugin“ />
</plugins>
</configuration>
-
Override the onload event of form and load the plugins as follows.
protected
override
void OnLoad(EventArgs e)
{
base.OnLoad(e);
LoadPlugins();
}
private
void LoadPlugins()
{
pluginList = (ArrayList)ConfigurationSettings.GetConfig(“plugins”);
}
-
Now in the button click event call the plugin’s execute task method.
private
void btnLog_Click(object sender, EventArgs e)
{
foreach (IPlugin plugin in pluginList)
{
plugin.ExecuteTask(textBox1.Text);
}
}
- Place the MessagePlugins dll or class library in the bin directory of windows application and run the application to test.
For the complete code please email me at ovaismehboob@yahoo.com.
i like it
Sweet web site , super design , real clean and utilize pleasant.
I was studying some of your content on this site and I believe this internet site is really instructive! Continue posting.
I was reading some of your articles on this website and I conceive this site is really informative! Keep on putting up.
I saw a lot of website but I conceive this one holds something special in it. “Truth always originates in a minority of one, and every custom begins as a broken precedent.” by Lady Nancy Astor.
Keep up the excellent work , I read few content on this web site and I believe that your blog is really interesting and holds circles of wonderful info .
As I website owner I conceive the content material here is rattling excellent , appreciate it for your efforts.
Awesome info over again! Thanks!
Following study a handful of the websites on your personal internet web site now, and that i genuinely like your technique for blogging. I bookmarked it to my bookmark site list and you are going to be checking back soon. Pls look at my internet page too and told me what you believe.
Hi there very cool blog!! Man .. Excellent .. Superb .. I will bookmark your web site and take the feeds also…I am happy to search out so many helpful information right here in the submit, we need work out more strategies on this regard, thanks for sharing.
I have recently started a blog, the information you offer on this web site has helped me greatly. Thanks for all of your time & work.
I like the helpful information you provide for your articles. I’ll bookmark your weblog and take a look at once more right here regularly. I’m somewhat certain I will learn lots of new stuff right right here! Best of luck for the following!
I would like to thank you for the efforts you have put in writing this web site. I’m hoping the same high-grade website post from you in the upcoming as well. Actually your creative writing abilities has encouraged me to get my own site now. Actually the blogging is spreading its wings quickly. Your write up is a great example of it.
I truly appreciate this post. I’ve been looking everywhere for this! Thank goodness I found it on Bing. You’ve made my day! Thank you again!
I think other web site proprietors should take this site as an model, very clean and excellent user friendly style and design, let alone the content. You are an expert in this topic!
I dugg some of you post as I cerebrated they were invaluable very beneficial
I’ve recently started a site, the information you provide on this site has helped me greatly. Thanks for all of your time & work. “It is no use saying, ‘We are doing our best.’ You have got to succeed in doing what is necessary.” by Sir Winston Churchill.
I will immediately grasp your rss as I can not find your email subscription hyperlink or newsletter service. Do you have any? Kindly let me understand so that I may subscribe. Thanks.
Wohh just what I was looking for, regards for posting . “Be nice to everyone on your way to the top because you pass them all on the way down.” by Fred Hufnagel, Sr..
I like this web site very much, Its a really nice berth to read and receive information. “Everything is vague to a degree you do not realize till you have tried to make it precise.” by Bertrand Russell.
I would like to express my appreciation to the writer just for bailing me out of such a dilemma. After surfing through the the web and finding methods which are not helpful, I thought my entire life was done. Living devoid of the approaches to the difficulties you’ve resolved through your main article is a crucial case, and those which may have negatively affected my career if I had not come across your blog post. The training and kindness in maneuvering all the things was crucial. I’m not sure what I would’ve done if I had not encountered such a solution like this. It’s possible to at this moment look forward to my future. Thanks a lot so much for your specialized and amazing guide. I will not hesitate to endorse the website to any person who needs counselling on this matter.
Merely wanna say that this is handy , Thanks for taking your time to write this.
F*ckin’ tremendous issues here. I am very glad to peer your article. Thanks a lot and i’m taking a look forward to touch you. Will you please drop me a mail?
Its such as you read my mind! You seem to grasp so much about this, such as you wrote the e-book in it or something. I think that you can do with a few % to pressure the message home a little bit, however other than that, this is great blog. A great read. I’ll certainly be back.
I am extremely inspired with your writing skills and also with the layout on your blog. Is that this a paid theme or did you modify it your self? Anyway stay up the nice high quality writing, it’s rare to peer a nice blog like this one nowadays.
I view something really interesting about your website so I saved to bookmarks .
I like what you guys are up also. Such smart work and reporting! Keep up the superb works guys I’ve incorporated you guys to my blogroll. I think it will improve the value of my website :).
I think you have mentioned some very interesting points , regards for the post.
What’s Taking place i am new to this, I stumbled upon this I have discovered It absolutely useful and it has aided me out loads. I hope to give a contribution & assist different customers like its aided me. Great job.
You got a very fantastic website, Gladiola I found it through yahoo.
I was reading some of your blog posts on this website and I conceive this internet site is very instructive! Continue putting up.
I really like your writing style, superb information, appreciate it for posting :D. “In university they don’t tell you that the greater part of the law is learning to tolerate fools.” by Doris Lessing.
Really nice pattern and fantastic content, practically nothing else we want :D.
We are a bunch of volunteers and opening a new scheme in our community. Your web site provided us with useful information to paintings on. You have done a formidable process and our entire neighborhood will probably be thankful to you.
I love the efforts you have put in this, regards for all the great blog posts.
Great blog! I really love how it’s easy on my eyes as well as the information are well written. I am wondering how I could be notified whenever a new post has been made. I have subscribed to your rss feed which ought to do the trick! Have a nice day!
hallo guys!! gorgeous site!
Servile flattery is no respect. Appropriate criticism is a much better kind of respect. And it can take us further.
I really loved the post so I used my Digg account to digg it.. Such a type of blog post will definitely click to numerous viewers. A good article and useful for its written content. Many thanks for sharing it up! pleasing .
I truly appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thx again!
I visited a lot of website but I conceive this one holds something special in it.
Its such as you learn my thoughts! You appear to understand a lot approximately this, like you wrote the e-book in it or something. I think that you just could do with a few percent to drive the message home a little bit, however instead of that, this is wonderful blog. A fantastic read. I will definitely be back.
I just checked the Nudia page on iTunes under podcasts to manually download and its not showing up in the list. Jorges interview is first on the list, next is the Sunset on the Beach trailer.
Hello! I simply want to give a huge thumbs up for the good info you have right here on this post. I will probably be coming again to your weblog for more soon.
You have noted very interesting details! ps decent website.
Great post, I conceive website owners should larn a lot from this website its rattling user genial . “My father always told me, ‘Find a job you love and you’ll never have to work a day in your life.'” by Jim Fox.
I believe this internet site contains some very good information for everyone. “Billy T-T-T-T-Today, Junior” by Billy Madison.
I was reading through some of your articles on this site and I conceive this web site is rattling informative! Retain putting up.
As soon as I found this website I went on reddit to share some of the love with them.
Great write-up, I’m normal visitor of one’s web site, maintain up the nice operate, and It’s going to be a regular visitor for a long time.
Thank you so much with regard to giving me an update on this matter on your web page. Please realise that if a brand new post appears or if any improvements occur about the current post, I would be thinking about reading more and finding out how to make good use of those strategies you share. Thanks for your efforts and consideration of other people by making your blog available.
Respect to website author , some good entropy. “To have a grievance is to have a purpose in life.” by Alen Coren.
I am sure this article has touched all the internet visitors, its really really fastidious post on building up new web site.
I really appreciate this post. I have been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thank you again!
I enjoy the efforts you have put in this, appreciate it for all the great articles.
I wanted to visit and allow you to know how really I liked discovering your site today. I would consider it a great honor to operate at my business office and be able to make real use of the tips provided on your web site and also get involved in visitors’ responses like this. Should a position of guest author become available at your end, please let me know.
Some truly fantastic content on this website , thanks for contribution.
I truly enjoy examining on this internet site , it contains wonderful blog posts.
Excellent work over again. I am looking forward for your next post=)
Its excellent as your other blog posts : D, thankyou for posting . “You can’t have everything. Where would you put it” by Steven Wright.
Hi to all, the YouTube record that is posted at at this place has genuinely pleasant quality along with pleasant audio quality
You have mentioned very interesting details! ps nice internet site.
Unquestionably consider that which you stated. Your favorite reason appeared to be at the web the simplest factor to understand of. I say to you, I certainly get irked whilst other people consider worries that they just do not recognize about. You managed to hit the nail upon the top and also defined out the entire thing without having side effect , other folks could take a signal. Will probably be back to get more. Thank you!
As I website owner I conceive the subject matter here is real superb , regards for your efforts.
Just want to say your article is as amazing. The clearness for your put up is simply excellent and that i can suppose you’re a professional in this subject. Well along with your permission let me to snatch your feed to keep updated with impending post. Thanks a million and please keep up the gratifying work.
Absolutely indited written content , thanks for entropy.
I’m having a small annoyance. I’m unable to subscribe to your rss feed for some reason. I’m using google reader by the way.
Very good info can be found on weblog .
Many thanks for being my personal teacher on this theme. I enjoyed your article very much and most of all enjoyed how you handled the aspect I regarded as controversial. You’re always really kind towards readers really like me and assist me to in my everyday living. Thank you.
What’s up everyone, it’s my first go to see at this website, and piece of writing is actually fruitful in favor of me, keep up posting such articles.
The Zune concentrates on being a Portable Media Player. Not a web browser. Not a game machine. Maybe in the future it’ll do even better in those areas, but for now it’s a fantastic way to organize and listen to your music and videos, and is without peer in that regard. The iPod’s strengths are its web browsing and apps. If those sound more compelling, perhaps it is your best choice.
Great post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!
I regard something really interesting about your weblog so I bookmarked .
You are my inhalation , I possess few blogs and infrequently run out from to brand.
You have observed very interesting details! ps nice site.
Can I just say what a aid to find somebody who truly is aware of what theyre speaking about on the internet. You undoubtedly know the way to deliver a difficulty to mild and make it important. More people have to read this and perceive this side of the story. I cant believe youre no more well-liked because you undoubtedly have the gift.
I found your weblog website on google and check a number of of your early posts. Proceed to keep up the excellent operate. I just extra up your RSS feed to my MSN News Reader. In search of ahead to studying more from you later on!…
Great post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!
My brother recommended I might like this blog. He was totally right. This post actually made my day. You can not imagine just how much time I had spent for this info! Thanks!
Cool stuff
I wanted to write a small message to say thanks to you for all of the marvelous strategies you are placing on this website. My particularly long internet investigation has at the end been recognized with reasonable insight to talk about with my guests. I would point out that most of us readers actually are very much endowed to be in a great website with so many outstanding professionals with interesting basics. I feel very happy to have used your web site and look forward to many more amazing minutes reading here. Thanks a lot once again for everything.