- 浏览: 34501 次
- 性别:
- 来自: 北京
文章分类
最新评论
Simple Introduction to Extensible Applications with the Managed Extensions Framework
Recently my team has been working on the Managed Extensions Framework (MEF)... I have gotten a chance to explain the concept to folks and I think I have discovered a way to talk about MEF that folks can easily get. So I thought I'd spend a little time walking through a *very* simple MEF example as a way to introduce folks to the power of extensible applications in general, and MEF in particular.
BTW, you can download the current MEF CTP and the final working sample.
Background
Let's start with the most simple example: Hello World!
1: using System;
2:
3: class Program
4: {
5: public void Run()
6: {
7: Console.WriteLine("Hello World!");
8: Console.ReadKey();
9: }
10: static void Main(string[] args)
11: {
12: Program p = new Program();
13: p.Run();
14: }
15: }
Now, you might now always want to print the same string, so let's refactor slightly to pull the string out..
1: public string Message { get; set; }
2:
3: public void Run()
4: {
5: Console.WriteLine(Message);
6: Console.ReadKey();
7: }
This looks nice, now we need to add the message... well, the actual text is a separate concern, as such it should be in a different class. Such as:
1: public class SimpleHello
2: {
3: public string Message
4: {
5: get
6: {
7: return "hello world!!";
8: }
9: }
10: }
Now we simply need wire these up:
1: public void Run()
2: {
3: SimpleHello hello = new SimpleHello();
4: Message = hello.Message;
5:
6: Console.WriteLine(Message);
7: Console.ReadKey();
8: }
This works, but something looks odd about line 3 and 4... We have introduced tight coupling back.. What we really want to do is externalize lines 3 and 4, so they can be controlled without effecting the rest of the logic of the program.
Enter MEF
一、基本用例
STEP 1:
Add a Reference to the System.ComponentModel.Composition.dll assembly found in the bin directory of the MEF zip.
Add
1: using System.ComponentModel.Composition;
Now in the Program class, we need to import a value for Message -- that is, we want to specify that someone outside of this program needs to supply a message. Then we need to remove our tight coupling. Note in line 4-5 and we saying we want to import the value for Message. Here I am showing doing it by type (string).. because basic types such as strings might be pretty common, consider using a named import such as [Import("Message")]
1: class Program
2: {
3:
4: [Import]
5: public string Message { get; set; }
6:
7: public void Run()
8: {
9: // SimpleHello hello = new SimpleHello();
10: //Message = hello.Message;
11:
12: Console.WriteLine(Message);
13: Console.ReadKey();
14: }
STEP 2:
Now in the SimpleHello class we need to export the Message property. This tells the system it can be used to satisfy requirements. Notice line 3 and 4 and am marking it with an Export attribute.. Again, this exports it by type (string in this case). As with above, you might want to do it with an explicit name for a more real world example [Export("Message")]
1: public class SimpleHello
2: {
3: [Export]
4: public string Message
5: {
6: get
7: {
8: return "hello world!!";
9: }
10: }
11: }
STEP 3:
Now we need to tell MEF to wire these up for us.
1: public void Run()
2: {
3: //SimpleHello hello = new SimpleHello();
4: //Message = hello.Message;
5: var catalog = new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());
6: var container = new CompositionContainer(catalog.CreateResolver());
7: container.AddPart(this);
8: container.Compose();
9:
10:
11: Console.WriteLine(Message);
12: Console.ReadKey();
13: }
In line 5, we create a catalog -- that tells MEF where to look for imports and exports. In this case, we are saying the currently running assembly. There are tons of different parts catalogs, we will look at some later and you can of course build your own.
in line 6, we create a Composition container-- this is effectively the soup that all the different parts will be wired up together.
In line 7, we add this instance of Program to the container so that its dependencies get wired up.
In line 8, we do the compose magic. this is where the Message property of Program gets set.
Notice, in this case the wire up is done by matching types (String to String)... clearly that isn't always the right way, we will look at other ways to wire up later.
Run it and you get the expected output of "hello world!".
二、小小的扩展
STEP 1:
Now let's add another message, just to be a little more fun...
1: public class MoreMessages
2: {
3: [Export]
4: public string FunMessage
5: {
6: get
7: {
8: return "This is getting fun!";
9: }
10: }
11: }
Now run.. It blows up! Why? Well, let's look at the exception:
System.ComponentModel.Composition.CompositionException Error : Multiple exports were found that match the constraint '(composableItem.ContractName = \"System.String\")'. The import for this contract requires a single export only."
STEP 2:
From the error it looks like we provided too many ways to satisfy the Import... MEF didn't know which one to pick. Of course you can programmatically get in there and help, you can also just remove the export from one of the messages... but more fun, you can actually tell MEF you are able to deal with zero or more results. Change the Message property of Program as follows:
1: [Import]
2: public IEnumerable<string> Messages { get; set; }
Notice we changed the return type to be a collection of strings rather than just one string.
Now change the usage code slightly and we get:
1: class Program
2: {
3:
4: [Import]
5: public IEnumerable<string> Messages { get; set; }
6:
7: public void Run()
8: {
9: //SimpleHello hello = new SimpleHello();
10: //Message = hello.Message;
11: var catalog = new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());
12: var container = new CompositionContainer(catalog.CreateResolver());
13: container.AddPart(this);
14: container.Compose();
15:
16: foreach (var s in Messages)
17: {
18: Console.WriteLine(s);
19: }
20:
21:
22: Console.ReadKey();
23: }
输出为:
hello world!!
This is getting fun!
Wow -- we get both messages! Pretty cool...
相关推荐
MEF-报告:数字时代的个人隐私和对品牌的长期信任(英文)-2020.8-11页2020精品报告.pdf
使用MEF的即插即用服务 请参阅相关的博客文章
VS MEF(Visual Studio的托管可扩展性框架风格) 特征 您现有的MEF零件的新的,更快的主机 重用您已经在使用的MEF属性 ExportFactory支持创建具有范围有效期(即共享边界)的子容器 文献资料
bak-R-ız 组 这是我们的
引导程序 MEF Autofac应用程序引导程序
MEF-Portfolio:民兵股票基金投资组合的数据可视化
这是目前metro ethernet 技术最权威的标准met,该文档是mef10.2
SOMIM2020-MEFenPython:使用MED的Desarrollo de ejemplo de MEF
MEF框架源代码 MEF框架源代码 MEF框架源代码
实现了一个MEF可扩展框架的简易例子,可参考MEF是如何实现并工作的
vs-mef, Visual Studio 使用的托管扩展性框架( MEF ) 实现 ( 管理扩展性框架工作室的视觉风格) 特性一个新的,更快的主机,为你现有的MEF部件重用已经使用的MEF属性ExportFactory<T> 支持创建具有作用域生命周期的子...
MEF 是用于 Play Java 应用程序的快速 TDD 框架。 它基于此处描述的 Robert Martin“实体边界交互器”架构。 此架构的目标是从应用程序代码中删除平台依赖性。 也就是说,删除对数据库和网络的依赖。 您的应用程序...
管理的用户界面用于C#桌面应用程序的高级托管环境。介绍ManagedUI(被管理用户界面)是一个用C#编写的.net框架库,可以视为创建非常高级的C#桌面应用程序的入门库。 如果一个项目需要像这样: 可编辑和动态菜单项...
MEF 论坛 E-Access service
,李玉凤,黄玉凤 1说明令人敬畏的微光图像增强方法的集合。维护论文,代码和数据集。 2相关工作 2.1数据集VV [] SID []大声笑[] NPE [] MEF [] CID [] SCIE [] LIME [] DICM [] BIMEF [] ExDark [] MIT-Adobe FiveK...
VV,LIME,NPE系列,DICM,MEF 仅低照度图像而没有相应的高照度地面实况 感谢收集 SID 下载:+或 麻省理工学院-Adobe FiveK (仅使用专家C的输出) ExDARK 仅低光图像,没有相应的高光地面真相 哈哈 下载:或 赛斯 ...
城域以太网系列MEF6-1协议,EVC分类详解,基础知识
MEF(Managed Extensibility Framework)简单实现
在WPF中MEF的应用,有简单实例和代码,MEF是主程序
MEF模块解耦,WCF Rest 扩展 基础示例