动态扩展应用程序的功能? [英] Dynamically extending features of an application?

查看:146
本文介绍了动态扩展应用程序的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近碰到开发可插拔应用模块使用的方式来时, ASP.NET MVC3 / 4,我喜欢这种方法的简单性。目前,我有我的应用程序结构如下:

I recently came across a way to develop pluggable application modules when using ASP.NET MVC3/4 and I loved the simplicity of the approach. Currently, I have my applications structured as follows:

因此​​,任何人想要开发我的应用程序的扩展,遵循从上面的教程并创建停留在领域文件夹中的延伸。我想,当新的领域(如新项目创建)的加入, .PDB 文件被创建并放置在目录。我的问题是这样的:

Therefore, anyone wanting to develop an extension for my application, follows the approach from the above tutorial and creates an extension that stays in the Areas folder. I figure that when new Areas (created as new projects) are added, .pdb files are created and placed in the bin directory. My question is this:


  • 一个人如何分配领域为可插拔模块?

  • 如何更改以下code,这样当有人删除了一个新的文件夹,该应用程序会自动将它拾起,并创建一个链接?又该插件笔者做启用此?

  • How does one distribute the Areas as pluggable modules?
  • How do I change the following code so that when someone drops a new Area into the bin folder, the application automatically picks it up and creates a link? And what should the plugin author do to enable this?

在我的 _Layout.cshtml (全球共享的布局),我做以下构建链接:

In my _Layout.cshtml (global shared layout), I do the following to construct the links:

<ul>
      <li>@Html.ActionLink("Area1", "Index", "Area1", new { Area = "Area1" }, null)</li>
      <li>@Html.ActionLink("Area2", "Index", "Area2", new { Area = "Area2" }, null)</li>
      <li>@Html.ActionLink("Area3", "Index", "Area3", new { Area = "Area3" }, null)</li>
</ul>

为简单起见,假定区域名称是唯一的。如何做到这一点有什么建议?

For simplicity, assume that the area names are unique. Any suggestions on how to do this?

推荐答案

不要在托管Web应用程序创建区域,但创建单独的项目,编译成单独的DLL的。 DLL的复制到你想使用它的任何Web应用程序。请记住,所有设置静态文件EmbeddedResource。

How does one distribute the Areas as pluggable modules?

Don't create the Areas in the hosting web app, but create separate projects, that compile to separate dll's. Copy the dll's to any web app where you want to use it. Remember to set all static files as "EmbeddedResource".

您可以使用MvcContrib PortableAreaRegistration的总线来发送邮件,从便携领域的命令的任何人总线上。这可以是托管Web应用程序,或者在理论上独立区域的可发送短信给对方。

You can use MvcContrib PortableAreaRegistration's "Bus" to send messages, commands from the portable area to anyone on the 'bus'. That can be the hosting web app, or in theory independent Area's can send message to each other.

创建了两个粗糙,但这个功能演示,code在GitHub上:

Created two crude, but functional demo of this, code on github:

<一个href=\"https://github.com/AkosLukacs/PluggablePortableAreas\">https://github.com/AkosLukacs/PluggablePortableAreas

<一个href=\"https://github.com/AkosLukacs/PluggablePortableAreasMVC4\">https://github.com/AkosLukacs/PluggablePortableAreasMVC4

首先,定义一个消息,可以携带您所需要的信息。只是,有一些特性(PluggablePortableAreas.Common \\ RegisterAreaMessage.cs)一个POCO:

First, you define a message that can carry the information you need. Just a POCO that has some properties (PluggablePortableAreas.Common\RegisterAreaMessage.cs):

public class RegisterAreaMessage : IEventMessage
{
    public string LinkText { get; private set; }
    public string AreaName { get; private set; }
    public string ControllerName { get; private set; }
    public string ActionName { get; private set; }
    //...
}

创建该消息类型的处理器(PluggablePortableAreas.Common \\ RegisterAreaEventHandler.cs):

Create a handler for that message type (PluggablePortableAreas.Common\RegisterAreaEventHandler.cs):

public class RegisterAreaEventHandler : MessageHandler<RegisterAreaMessage>{}

在这种情况下,MessageHandler的只是增加收到的消息为静态 ConcurrentBag&LT; RegisterAreaMessage&GT; 。您可以使用DI,如果你想,但希望保持简单。

In this case, the MessageHandler just adds the received messages to a static ConcurrentBag<RegisterAreaMessage>. You can use DI, if you want to, but wanted to keep it simple.

您可以从便携式地区发送消息像这样(区\\ DemoArea1 \\区\\ demo1的\\ Demo1AreaRegistration.cs):

You can send the message from the portable area like this (Areas\DemoArea1\Areas\Demo1\Demo1AreaRegistration.cs):

 //the portable area sends a message to the 'bus'
 bus.Send(new RegisterAreaMessage("Link to the Demo area", AreaName, DefaultController, DefaultAction));

动态添加的链接通过迭代的消息的集合(PluggablePortableAreas.Web \\查看\\ Shared_Layout.cshtml)显示:

The dynamically added links are displayed by iterating over the collection of messages(PluggablePortableAreas.Web\Views\Shared_Layout.cshtml):

                @foreach(var sor in PluggablePortableAreas.Common.RegisterAreaEventHandler.RegisteredAreas) {
                <li>@Html.ActionLink(sor.LinkText, sor.ActionName, sor.ControllerName, new{Area=sor.AreaName}, null)</li>
                }

一件事照顾:使用完全合格区域名称。如果你没有明确指定的区域名称,MVC假定它是当前区域。没有问题,而不领域,但第二个将指向/ path_to_your_app / CurrentArea /家庭而不是/ path_to_your_app /家庭

<li>@Html.ActionLink("Home", "Index", "Home", new { Area = "" }, null)</li>
<li>@Html.ActionLink("I don't work from the portable area!", "Index", "Home")</li>

甚至还有一件事要注意!

在VS开发服务器感觉有点古怪,有时便携区域不加载。在完整的IIS寿可靠工作的...

Even one more thing to note!

The development server in VS feels a bit "erratic", sometimes the portable area doesn't load. Works reliably in full IIS tho...

这篇关于动态扩展应用程序的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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