DevMania
DevMania

Reputation: 2341

how to create pluggable ASP.Net website?

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 ?

Upvotes: 8

Views: 3829

Answers (4)

JonnyBoats
JonnyBoats

Reputation: 5187

If you would like to see a real, open source application that impliments this archecture take a look at DotNetNuke.

Upvotes: 1

Ray Womack
Ray Womack

Reputation: 1010

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.

Upvotes: 12

boj
boj

Reputation: 11385

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:

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.

Upvotes: 2

Steve Willcock
Steve Willcock

Reputation: 26839

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.

Upvotes: 1

Related Questions