简体   繁体   English

如何创建可插拔的ASP.Net网站?

[英]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]. 是不是有一个更简单的解决方案,就像我看到的blogengine插件创建系统你只需要用[Extension]来装饰类插件。

I am kind of mid core developer, so I was thinking of base class, inheritance, interfaces, what do you think ? 我是中级核心开发人员,所以我在想基类,继承,接口,你怎么看?

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. 第1步:定义插件将实现的接口。

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

Step 2: Create a plugin that implements IPlugin. 第2步:创建一个实现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. 第3步:运行插件。

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 创建一个插件主机,在其自己的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 使用反射,接口,IL发出的thunk或委托来完成后期绑定工作

if your design so dictates. 如果你的设计如此规定。

It's valuable to separate technical and architecturas perspectives: 将技术和架构视角分开是很有价值的:

  • In code level MEF (Managed Extensibility Framework) is a good start. 在代码级别, MEF (Managed Extensibility Framework)是一个良好的开端。 Here is a simple example. 是一个简单的例子。
  • Any other DI (Dependency Injection framework) can work well to (ie. Unity) 任何其他DI(依赖注入框架)都可以很好地工作(即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. 如果你发现一些令人反感的话,ofcoz就会阅读这篇文章。

Edit 编辑

if you are searching for an extensible blog engine then try Blog Engine first. 如果您正在搜索可扩展的博客引擎,请先尝试使用Blog Engine It's from ASP.NET community. 它来自ASP.NET社区。

This sounds like a job for the Managed Extensibility Framework from Microsoft. 这听起来像是Microsoft的Managed Extensibility Framework的一项工作。 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. 有关于如何在网站上使用此链接的指南链接。

如果您希望看到一个真实的,开源的应用程序,可以看看这个结构,请看看DotNetNuke

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM