Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create pluggable ASP.Net website?

Tags:

c#

asp.net

What are the best practices to create a site, with ability to develop plugins for it?

Like you want to create a blog module, and you want users or co-developers to add plugins to extend this module functionality.

Update: Thanks for the ultra speed answers, but I think this is over kill for me. Isn't there a simpler solution, like I have seen blogengine plugin creation system is you just have to decorate the class plugin with [Extension].

I am kind of mid core developer, so I was thinking of base class, inheritance, interfaces, what do you think ?

like image 580
DevMania Avatar asked Apr 11 '09 21:04

DevMania


3 Answers

Edit

I completely rewrote my answer based on your question edit.

Let me show you just how easy it is to implement a plugin architecture with just the minimal steps.

Step 1: Define an interface that your plugins will implement.

namespace PluginInterface
{
    public interface IPlugin
    {
        string Name { get; }
        string Run(string input);
    }
}

Step 2: Create a plugin that implements IPlugin.

namespace PluginX
{
    using PluginInterface;

    public class Plugin : IPlugin
    {
        public string Name
        {
            get { return "Plugin X"; }
        }

        public string Run(string input)
        {
            return input;
        }
    }
}

Step 3: Run the plugin.

namespace PluginTest
{
    using System;
    using System.IO;
    using System.Runtime.Remoting;
    using PluginInterface;

    class Program
    {
        static void Main( string[] args )
        {
            string pluginFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PluginX.dll");
            ObjectHandle handle = Activator.CreateInstanceFrom(pluginFile, "PluginX.Plugin");

            IPlugin plugin = handle.Unwrap() as IPlugin;

            string pluginName = plugin.Name;
            string pluginResult = plugin.Run("test string");          

        }
    }
}

Keep in mind, this is just the basic, most straightforward example of a plugin architechure. You can also do things such as

  • create a plugin host to run your plugin inside of it's own AppDomain
  • choose either interfaces, abstract classes, or attributes to decorate your plugins with
  • use reflection, interfaces, IL-emitted thunks or delegates to get the late binding job done

if your design so dictates.

like image 100
Ray Womack Avatar answered Oct 25 '22 09:10

Ray Womack


It's valuable to separate technical and architecturas perspectives:

  • In code level MEF (Managed Extensibility Framework) is a good start. Here is a simple example.
  • Any other DI (Dependency Injection framework) can work well to (ie. Unity)

And it's good to see this problem in architectural level:

  • Web Client Software Factory from p&p. Here are not only technical but arcihtectural informations about "How to create composite web applications?". See examples.. There is Modularity Boundle package.
  • Spring Framework.

I think it's a fast and efficient if you read&try some of those frameworks. And ofcoz read the source if you find something interessing.

Edit

if you are searching for an extensible blog engine then try Blog Engine first. It's from ASP.NET community.

like image 20
boj Avatar answered Oct 25 '22 10:10

boj


This sounds like a job for the Managed Extensibility Framework from Microsoft. It's in a preview release at the moment but it would seem to be a better bet than rolling your own framework for this. There are links to guides about how to use this on the site there.

like image 24
Steve Willcock Avatar answered Oct 25 '22 09:10

Steve Willcock