架构扩展/插件通信 [英] Architecture for extension/plugin communication

查看:210
本文介绍了架构扩展/插件通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在加载插件的问题就解决了​​(在.NET中通过MEF中出的情况下),下一步要解决的就是与他们沟通。最简单的方法是实现一个接口,并使用插件实现,但有时插件,只需要扩展应用程序的工作方式,可能有大量的扩展点。

Once the problem of loading plugins is solved (in .NET through MEF in out case), the next step to solve is the communication with them. The simple way is to implement an interface and use the plugin implementation, but sometimes the plugin just needs to extend the way the application works and there may be a lot of extension points.

我的问题是关于如何处理与扩展点。我见过这样做,不同的方式,但我不知道每个人的优点和缺点,如果有更多更好的方式来做到这一点:

My question is about how to deal with that extension points. I've seen different ways of doing that but I'm not sure of the pros and cons of each one and if there are more and better ways to accomplish this:

  • 活动:添加静态活动,所有我们要扩展的东西。举例来说,如果我想添加自定义验证一个User类,我可以添加一个OnValidation静态的事件处理程序和插件事件添加到它时,它的构造。
  • 消息:具有总线和消息。该插件可以订阅特定的消息,做的东西,当其他一些类发布该消息。该消息应包含在该插件可以正常工作范围内。在确认的情况下,逻辑层将发布UserValidation消息,收到消息时,插件就会采取行动。
  • 接口:主机应用程序负责调用所有实现某些接口,并给他们当前操作的情况下的插件。在确认的情况下,该插件可以实现IValidator或IUserValidator与验证(对象上下文)方法。

你有没有用户暴露的途径之一?哪一个工作最适合你?

Have you ever user one of the exposed approaches? Which one worked best for you?

和我可以告诉大家,我们的应用程序是一个可扩展的核心(用户,罗拉和内容管理),以在此基础之上建立我们的客户的具体内容为中心的Web应用程序。一切都建立在ASP.NET MVC。

And before you ask, our application is an extensible core (user, rola and content management) to build our client specific content centric web applications on top of that. Everything built on ASP.NET MVC.

推荐答案

一个关键的设计决策是分析和获得的插件如何不同将是海誓山盟的清晰画面。

A key for your design decision is to analyze and get a clear picture of how different the plugins will be from eachother.

例如。当静态活动时,您可能要定义每一个事件作为某种形式的令牌,枚举,对象等,不必定义一组新的事件为每个插件作品自然对你的整个设计,特别是在松耦合的条款及重用。

E.g. when dealing with static events, you will probably have to define each event as some form of token, enum, object etc. Having to define a new set of events for each plugin naturally works against your whole design, particularly in terms of loose coupling and reuse.

如果你的插件有很大的不同,你可能会受益于有,因为你总线/通讯架构在这种情况下,可以引入沟通交流,该插件可以订阅站点/类别。即一系列事件和消息可以是在一定的兴趣域。这里注意,在一个特定类别的通信仍然可以利用静态事件,因此这两个替代方案不是相互排斥的。

If your plugins are very different you might benefit from having a bus/messaging architecture since you in such case can introduce domains/categories of communication exchange, which the plugins can subscribe to. I.e. a range of events and messages can be in a certain interest domain. Note here that communication within a certain category can still utilize static events, so those two alternatives are not mutually exclusive.

通过插件实现的直接接口是在我的经验插件架构的最严格的方法。扩展插件接口通常意味着code修改在两个插件和供应商。你需要有哪些你知道你的应用程序可以住在相当长的一段时间了坚实的通用接口。

Direct interfaces implemented by the plugins is in my experience the strictest approach of plugin architecture. Extending the plugin interface usually implies code modification at both plugin and provider. You need to have a solid general interface which you know your application can live on for quite some time.

这可能是更容易为你处理的设计,打破它分解成两个方面 - 通信信道协议。静态事件处理是一个协议的问题,而总线通讯和直接接口是一个渠道的问题。

It may be easier for you to deal with the design by breaking it down into two aspects - communication channel and protocol. Static event handling is a protocol issue, while bus-messaging and direct interfaces is a channel issue.

一般来说,我会说,该协议是最难从一开始就正确地设计,因为你可能不会有一个坚实的感觉如何可以一般或特定划清界线。

Generally I would say that the protocol is the hardest to design correctly from the beginning, since you may not have a solid feel for how general or specific you can draw the line.

编辑:拉斯取得了他的评论很重要的一点 - 如果你的平台支持的异常,则可以使用直接的接口时,集中了大量的错误处理,不必处理错误缓解插件,是通用的,也许outisde其特定领域(如插件加载错误或文件打开失败)。然而,这样的好处会显得褪色,如果你要每次添加插件时保持接口。最坏的情况是,当接口开始变得一路上不一致,因为你并没有意识到他们应该支持从一开始就。重构整个界面设计时已经被设想插件大量不是一件容易的任务。

Lars made an important point in his comment - if your platform supports exceptions, you can centralize a lot of the error handling when using direct interfaces, relieving the plugins from having to handle errors that are generic and perhaps outisde their particular domain (e.g. "plugin load error", or "file open failed"). However, such benefits will seem to fade if you have to maintain interfaces each time you add plugins. Worst case is when the interfaces start becoming inconsistent along the way because you didn't realize what they should support from the beginning. Refactoring the entire interface design when a substantial amount of plugins already have been conceived is not an easy task.

这篇关于架构扩展/插件通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆