使用 ASP.NET MVC 实现 MEF?

发布于 2024-07-17 01:34:00 字数 331 浏览 8 评论 0原文

我想了解是否有人有使用 MEF(托管可扩展框架(微软的新插件框架)与 ASP.NET MVC 的经验或想法。我需要创建一个标准的 ASP.NET MVC,我已经有了。但我需要提供额外的功能,即视图和控制器等,具体取决于我是否添加插件,它不需要动态编译,即源代码...而是我放入系统的 DLL。

有什么方法可以实现吗?在应用程序启动时动态加载 DLL,然后将视图和控制器与主系统合并?我不知道我是否走在正确的轨道上,

然后,我想在应用程序附带的“标准”视图中 。 ,我可以使用“IF THEN”来查明插件是否已加载并在用户控件中合并。

好吧,我在这里大声说话,但我想您明白我的意思

吗?

I am trying to find out if anyone has any experience or ideas of using MEF (Managed Extensible Framework (Microsoft's new plugin framework) with ASP.NET MVC. I need to create a standard ASP.NET MVC, which I have. But I need to offer additional functionality i.e. Views and Controllers, etc, depending on if I add a plugin. It doesn't need to be dynamically compiled i.e. source code... but a DLL that i put into the system..

Is there any way to dynamically load a DLL when the app starts, and then MERGE a VIEWS and CONTROLLERS with the main system? I don't know if i am on the right track here.

Then, I suppose in the "STANDARD" views that come with the app, I can use an "IF THEN" to find out if a plugin is loaded and MERGE in a user control.

Well, I'm talking out loud here, but I think you understand what I am getting at.

Any ideas?

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

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

发布评论

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

评论(6

浪漫人生路 2024-07-24 01:34:01

我已经使用 MVC 和 MEF 与强类型视图组合了一个可插入框架: http://www.thegecko.org/index.php/2010/06/pluggable-mvc-2-0-using-mef-和-强类型-视图/

I have put together a pluggable framework using MVC and MEF with strongly-typed views over at : http://www.thegecko.org/index.php/2010/06/pluggable-mvc-2-0-using-mef-and-strongly-typed-views/

幸福%小乖 2024-07-24 01:34:00

运气好吗? 我正在阅读 这个并认为这就是您正在寻找的。
我的 MEF 经验值正好为零,但看起来很有希望。 我希望我能在家里挤出几个小时的空闲时间来尝试一下。 以这种方式创建某种模块化的 MVC“框架”将非常有用。

就我目前正在做的项目而言,我遇到以下问题:
多个站点具有相同的视图,只有其他 CSS 文件。 目前我必须复制视图,从而导致维护问题。
我希望可以使用 MEF 将这些视图放在中心位置。

any luck with that? I was reading this and think that's what you're looking for.
I have exactly zero xp with MEF, but it looks promising. I hope I can scrape some hours of free time together at home to experiment with that. It would be ultra usefull to create some kind of modularized MVC "framework" that way.

As for the current project I'm working on, I have the following problem:
Multiple sites with the same views, only other CSS files. Currently I have to duplicate the views resulting in a maintaince problem.
I'm hoping I can put these views in a central place using MEF.

策马西风 2024-07-24 01:34:00

我们在 ASP.NET MVC 中使用了大量的 MEF,尽管其中大部分位于控制器级别以下的级别,因为在我们的较低级别模块中使用 MEF 插件来检查权限和验证数据。

然而,我们也对控制器使用了更可组合的方法。 视图更加棘手,但我们实际上完全消除了常规 ASP.NET MVC 视图的使用,并将 Razor 视图存储在数据库中的片段中。 然后,我们的控制器在运行时向模板引擎请求视图,并将 ContentResult 呈现给响应,而不是返回 View("Viewname") 等。

我们的 MEF 插件都带有标识符属性,这些属性让我们在运行时进行级联覆盖以找出哪个插件/类应该用于给定的目的。 最简单的演示示例是,如果您考虑一个具有共同基础的应用程序,但部署到 50 多个实现,每个实现都可以选择覆盖核心功能。

因此,您可能有类似 IUserController 的东西,其中包含“登录”、“注销”等方法。

除了实际功能之外,我们还会向名为“SiteId”的接口添加一个只读 GUID 属性。 然后,每个实现都会对其预期的 SiteId 进行硬编码。 对于核心代码中的“默认”实现,它将返回“Guid.Empty”。

然后,当我们调用 MEF 并寻找要使用的 IUserController 实现时,我们会将所有这些实现导入到一个列表中,然后使用 LINQ 查询属性以确定要使用哪一个

var _currentUserController = _availableUserControllers.FirstOrDefault(
  c=>c.SiteId == AppSettings.SiteId);
if(_currentUserController == null){
    //There is no site-specific override
    _currentUserController = _availableUserControllers.FirstOrDefault(
      c=>c.SiteId == Guid.Empty);
}

:对于控制器,您最好的选择是查看网络上基于 MEF 的控制器工厂的一些实现。

然而,就像我说的,我们几乎所有这些都是在较低的级别上完成的,并且让我们的模块或控制器进行这种查找以确定要运行哪些插件。

We're using tons of MEF in ASP.NET MVC, though most of it sits at a level below the controller level, as in our lower level modules use MEF plugins for checking permissions and validating data.

However, we also use a more composable approach to our controllers too. Views are more tricky, but we actually completely eliminated the use of regular ASP.NET MVC views and store our Razor views in snippets in a database. Our controllers then ask a template engine for a view at runtime and render ContentResult to the response instead of returning View("Viewname"), etc.

Our MEF plugins all carry identifier properties that let us do a cascading override at runtime to figure out which plugin/class should be used for a given purpose. The easiest example to demonstrate would be if you think about an app that has a common base, but is deployed to 50+ implementations that each have the option to override the core functionality.

So, you might have something like an IUserController that includes methods for "Login", "Logout", etc.

In addition to that actual functionality, we'd add a read-only GUID property to the interface called "SiteId". Each implementation would then hard-code the SiteId it's intended for. For the "default" implementation in the core code, it would return "Guid.Empty".

Then, when we invoke MEF and go looking for which implementation of IUserController to use, we'd do an ImportMany of all of them into a List and then use LINQ to query the properties to figure out which one to use:

var _currentUserController = _availableUserControllers.FirstOrDefault(
  c=>c.SiteId == AppSettings.SiteId);
if(_currentUserController == null){
    //There is no site-specific override
    _currentUserController = _availableUserControllers.FirstOrDefault(
      c=>c.SiteId == Guid.Empty);
}

To do this with controllers, your best bet is to look at some of the implementations of MEF-based controller factories out there on the web.

However, like I said, we do nearly all of this at a level that's lower and have our modules or the controllers do this kind of lookup to determine which plugins to run.

扮仙女 2024-07-24 01:34:00

检查一下:

http://www.fidelitydesign.net/?p=104

模块化 ASP使用托管扩展性框架 (MEF) 的 .NET MVC,第一部分,作者:Matthew Abbott。

Check this :

http://www.fidelitydesign.net/?p=104

Modular ASP.NET MVC using the Managed Extensibility Framework (MEF), Part One by Matthew Abbott.

别在捏我脸啦 2024-07-24 01:34:00

这是一个疯狂的猜测。
您可以使用 MEF 来发现 IController 的工厂覆盖默认控制器工厂。 由于视图是按惯例发现的,因此您不必担心它们。

This is a wild guess.
You could overwrite the default controller factory with one that uses MEF to discover IControllers. Since Views are discovered by convention, you shouldn't have to worry about them.

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