简体   繁体   English

MEF:我应该把CompositionContainer放在哪里?

[英]MEF: Where should I put the CompositionContainer?

I have been using the Windsor IoC Container for my web-based application, to resolve the data access layer implementation the application should use. 我一直在使用Windsor IoC Container作为基于Web的应用程序,以解决应用程序应该使用的数据访问层实现。

The web application's UI will consist of pages, and each page consists of small units called portlets. Web应用程序的UI将由页面组成,每个页面由称为portlet的小单元组成。 (Their concept is somewhat similar to widgets.) These so-called portlets are basically web controls and can be configured in runtime for every page invidually. (它们的概念有点类似于小部件。)这些所谓的portlet基本上是Web控件,可以在运行时为每个页面进行静态配置。

The application will ship with some of these built-in, but I would like to enable extending it easily. 该应用程序将附带一些内置的,但我希望能够轻松扩展它。

I figured out that this mechanism is exactly what MEF is built for. 我发现这个机制正是MEF的基础。 So I decided to implement the system in such a way that it discovers portlets using MEF. 所以我决定以这样的方式实现系统,即它使用MEF发现portlet。 Then, I realized that it can also do what I currently use Windsor for, so I decided to ditch Windsor in favor of MEF. 然后,我意识到它也可以做我目前使用温莎的东西,所以我决定放弃温莎而不是MEF。

Obviously, I will have to use the DirectoryCatalog, which scans for the .dlls in the app's bin folder and returns everything I need. 显然,我将不得不使用DirectoryCatalog,它会扫描应用程序bin文件夹中的.dll并返回我需要的所有内容。

I read some tutorials, examples, and all questions regarding MEF in StackOverflow, as well. 我在StackOverflow中阅读了一些有关MEF的教程,示例和所有问题。 I figured that the easiest way to use MEF is through the PartInitializer which Glenn Block mentioned in his tutorials, but I realized that it is not in MEF. 我认为使用MEF最简单的方法是通过Glenn Block在他的教程中提到的PartInitializer,但我意识到它不在MEF中。 Actually, it is in the code I downloaded from CodePlex, but in a separate assembly, and only in source, not in binary form. 实际上,它是在我从CodePlex下载的代码中,但是在一个单独的程序集中,并且只在源代码中,而不是以二进制形式。 (Does this mean that it isn't a part of MEF? Or what's the point in putting it to a separate project?) Then, I realized that it is for Silverlight, so it doesn't really help me. (这是否意味着它不是MEF的一部分?或者将它放到一个单独的项目中有什么意义?)然后,我意识到它适用于Silverlight,所以它并没有真正帮助我。 (Or should I just compile that against .NET 3.5, or include it in my project, and I'm good to go?) (或者我应该只针对.NET 3.5进行编译,还是将其包含在我的项目中,我很高兴?)

So now I have a problem which is the following: where should I put the CompositionContainer in my application? 所以现在我遇到了以下问题:我应该将CompositionContainer放在我的应用程序中?

There is another thing I would like to consider: should I use only one CompositionContainer in the lifetime of the app, or I'm better off creating a container for every time when I need it? 还有一件事我想考虑一下:我应该在应用程序的生命周期中只使用一个CompositionContainer,或者我最好在每次需要时创建一个容器?

Good questions. 好问题。

In general in terms of questions about where to put the container, I recommend the following posts: http://blogs.msdn.com/nblumhardt/archive/tags/Container+Managed+Application+Design/default.aspx 一般来说,关于放置容器的位置的问题,我推荐以下帖子: http//blogs.msdn.com/nblumhardt/archive/tags/Container+Managed+Application+Design/default.aspx

In of MEF on the web, web-based apps are a bit tricker because of the request / response nature and scalability concerns. 在Web上的MEF中,由于请求/响应性质和可伸缩性问题,基于Web的应用程序有点琐碎。 For web you would likely want to have a hierarchy of containers, one root one for the application which is shared, as well as child contianers per-request. 对于Web,您可能希望拥有容器层次结构,一个用于共享的应用程序的根目录,以及每个请求的子contianers。 The child containers should live and die with the request in order to conserve resources. 子容器应该根据请求生存和死亡,以节省资源。 The shared container contains services that are shared by all callers. 共享容器包含所有呼叫者共享的服务。

You might check out these articles for more insight into how to do this: 您可以查看这些文章,以便更深入地了解如何执行此操作:

http://blogs.msdn.com/hammett/archive/2009/04/23/mef-and-asp-net-mvc-sample.aspx http://blogs.msdn.com/hammett/archive/2009/07/15/mef-and-asp-net-mvc-sample-updated.aspx http://mef.codeplex.com/wikipage?title=Parts%20Lifetime&referringTitle=Guide http://blogs.msdn.com/hammett/archive/2009/04/23/mef-and-asp-net-mvc-sample.aspx http://blogs.msdn.com/hammett/archive/2009/07 /15/mef-and-asp-net-mvc-sample-updated.aspx http://mef.codeplex.com/wikipage?title=Parts%20Lifetime&referringTitle=Guide

As far as PartInitializer, I would avoid using something like it unless you have to. 至于PartInitializer,我会避免使用类似的东西,除非你必须这样做。 ASP.NET provides sufficient hooks in the pipeline through HTTP Handlers, modules and such to let automatically compose on creation. ASP.NET通过HTTP处理程序,模块等在管道中提供了足够的钩子,以便在创建时自动编写。

The only place i would see using PI on the web would be possibly within a custom user control. 我在网上使用PI的唯一地方可能是自定义用户控件。 PI ships as part of Silverlight 4 and is not available in the box for .NET 4.0. PI作为Silverlight 4的一部分提供,在.NET 4.0的框中不可用。 I have created a usable version for .NET 4.0 which you can find here: http://cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Composition.Initialization.Desktop.zip 我已经为.NET 4.0创建了一个可用的版本,你可以在这里找到: http//cid-f8b2fd72406fb218.skydrive.live.com/self.aspx/blog/Composition.Initialization.Desktop.zip

HTH Glenn HTH Glenn

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

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