C# 中基于列表的发布订阅模式(Wordpress 挂钩/过滤器)

发布于 2024-12-25 09:07:30 字数 1694 浏览 0 评论 0原文

我有 PHP 背景,经常使用 Wordpress,我喜欢他们的插件架构的工作方式以及将事件与事件名称挂钩的能力。我喜欢它的最好的部分之一是能够在任何数据库值显示给最终用户之前*add_filter()*到它。我的问题是如何在 C#.NET 环境中复制整个插件架构?

第 1 部分: 为了创建插件,我研究了 MEF 框架 可能是最好的(托管扩展性框架 -http://mef.codeplex.com/)。这是专门为解决繁重的工作而设计的,让您能够监视新插件的目录、跟踪依赖项和其他正常的事情。 MEF 随 .NET 3.5+ 一起提供

第 2 部分 挂钩事件?我似乎找不到太多有关复制基于全局通道的事件系统的信息。根据我目前的情况,我需要一个发布/订阅模式(这并不难制作,因为您只需创建一些具体对象并为它们提供事件)。困难的部分是为每个事件指定一个“通道”名称,并使整个系统中的所有事件成为全局集合的一部分(中介者模式)。

复制:(http://codex.wordpress.org/Function_Reference/add_filter)

示例 1

// Add's my button to the end of the content
add_filter('the_content', 'my_plugin_button');

function my_plugin_button( $content ) {
    // Add's my button to the end of the content
    return $content . "<a href='#'>My button</a>";
}

示例 2

// Add a new admin menu item by hooking in
add_action('admin_menu', 'my_plugin_menu');

function my_plugin_menu() {
    add_options_page('My Plugin Options', 'My Plugin', 'manage_options', 'my-unique-identifier', 'my_plugin_options');
}

我希望您能支持我?我已经成功地在 Javascript 中复制了我需要的功能,甚至 jQuery 也有它们的 .on() 事件函数...相同的东西,但基于通道或列表...

我的 2 个示例:

谁能指出我正确的方向或者这对于 C# 来说是完全错误的方法吗?

I come from a PHP background and have used Wordpress quite a lot, I love how their plugin architecture works and the ability to hook events to event names. One of the best parts I like about it is being able to *add_filter()* to any database value just before it gets shown to the end user. My question is multi-part on how to replicate the whole plugin architecture in a C#.NET environment?

Part 1:
To create plug-ins I have researched the MEF framework would probably be the best (Managed Extensibility Framework -http://mef.codeplex.com/). This is designed specifically to take the grunt work out by giving you the ability to monitor directories for new plug-ins, tracking dependencies and other normal things. MEF ships with .NET 3.5+

Part 2
Hooking events? I can't seem to find much information about replicating a global channel based event system. From what I have upto yet I need a publish/subscribe pattern (which isn't that hard to make as you just create some concrete objects and give them events). The hard part is giving each event a 'channel' name and for all the events in the whole system to be part of a global collection (Mediator pattern).

To replicate: (http://codex.wordpress.org/Function_Reference/add_filter)

Example 1

// Add's my button to the end of the content
add_filter('the_content', 'my_plugin_button');

function my_plugin_button( $content ) {
    // Add's my button to the end of the content
    return $content . "<a href='#'>My button</a>";
}

OR

Example 2

// Add a new admin menu item by hooking in
add_action('admin_menu', 'my_plugin_menu');

function my_plugin_menu() {
    add_options_page('My Plugin Options', 'My Plugin', 'manage_options', 'my-unique-identifier', 'my_plugin_options');
}

I hope your all with me upto yet? I have managed to replicate the functionality I need in Javascript and even jQuery has their .on() event function... same thing but channel or list based...

My 2 examples:

Can anyone point me in the correct direction or is this the totaly wrong approach for c#?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神经大条 2025-01-01 09:07:30

我认为 NServiceBus 可以帮助您解决这些问题。 NServiceBus 的作者 Udi Dahan 也写了很多关于领域事件模式的文章,这是一个发布/订阅机制。

I think NServiceBus can help you a lot with these issues. Udi Dahan which is the author of NServiceBus has also written a lot of articles about Domain Event pattern, which is a publish/subscribe mechanism.

挥剑断情 2025-01-01 09:07:30

知道你发布这篇文章已经过去很长时间了,而且你可能已经构建了一些东西。然而我自己也一直在思考这样的事情。有两个选择 - 真的忘记 WordPress 并尝试构建更干净的东西 - WordPress 代码的底部一团糟 :D

或者这样:

function the_content()
{
        var result = get_the_content();
        // do other stuff...if you want to.
        execute_filters(ref result);
        execute_actions(ref result);

        return result;
}

function execute_filters(ref string result, string action_name)
{
     var filters = get_pre_filters(action_name);
     filters.ForEach(filter =>
     {
         /// somehow call the method name in filter.  PHP is generally global.  C# namespaced,      
         /// so you would need to think about that.
     }
}

function execute_actions(ref string result, string action_name)
{
      /// and so on....
}

当构建一些模仿 WordPress 的东西时,你需要记住 WordPress 插件的许多问题架构(在我个人看来)...似乎希望在每个页面上足够接近的位置运行每个插件,即使该页面与该插件无关。我曾经安装过一个插件,为每个页面调用添加了60个数据库查询,但没有使用。

当你构建它时,尝试并明智地思考它。尝试添加一种方法,仅让将在新设置的页面/帖子上使用的插件运行,例如在数据库中,帖子/页面对象上有一个“插件”字段,其中包含允许的插件列表在该页面上运行。这样您就不需要每次都检查所有插件来查看它是否要运行。

无论如何。希望你能有所收获。

Know it's been a long time since you posted this and you probably built something already. However I have been thinking about something like this myself. There are 2 options - really forget WordPress and try and build something much cleaner - it's a mess at the bottom of WordPress' code :D

Or this:

function the_content()
{
        var result = get_the_content();
        // do other stuff...if you want to.
        execute_filters(ref result);
        execute_actions(ref result);

        return result;
}

function execute_filters(ref string result, string action_name)
{
     var filters = get_pre_filters(action_name);
     filters.ForEach(filter =>
     {
         /// somehow call the method name in filter.  PHP is generally global.  C# namespaced,      
         /// so you would need to think about that.
     }
}

function execute_actions(ref string result, string action_name)
{
      /// and so on....
}

When building something to mimic WordPress, you need to remember many of the issues of WordPress' plugin architecture (in my personal opinion)... It seems to want to run every plugin near enough on every page even if that page has nothing to do with that plugin. I onced installed a plugin that added 60 database queries to each page call, and it wasn't used.

Try and think smart about it when you are building it. Try and add a way to only have the plugins that are going to get used on the page/post of your new setup to be run e.g. in your database have a "Plugins" field on the post/page object with a list of plugins allowed to run on that page. That way you won't need to check all the plugins each time to see if it wants to run.

Anyways. Hope you got something working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文