More Value of MEF
一、catalog基本使用
Ok, I think we can all agree that we added a little complexity if all we were going to do is factor what was in the same assembly. MEF really shines when you have separate independent groups working on different components. By definition these are often in different assemblies with cross no dependencies. To show how MEF supports this, let's add a new Class Library project to our solution. Call it ExternalMessages and add a reference to the System.ComponentModel.Composition.dll assembly.
STEP 1:
Add the following class.
1: using System;
2: using System.ComponentModel.Composition;
3:
4: public class Class1
5: {
6: [Export]
7: public string Message
8: {
9: get
10: {
11: return "I am starting to get it...";
12: }
13: }
14: }
STEP 2:
Now we need to wire up this class into the catalog... Notice in line 6, we change the catalog to look in a directory for the parts...
1: public void Run()
2: {
3: //SimpleHello hello = new SimpleHello();
4: //Message = hello.Message;
5: var catalog = new DirectoryPartCatalog(@"..\..\..\ExternalMessages\bin\Debug");
6: // new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly());
7: var container = new CompositionContainer(catalog.CreateResolver());
8: container.AddPart(this);
9: container.Compose();
10:
11: foreach (var s in Messages)
12: {
13: Console.WriteLine(s);
14: }
15:
16:
17: Console.ReadKey();
18: }
Note: DirectoryPartCatalog also supports relative paths that will look for a path under the current AppDomain.CurrentDomain.BaseDirectory. For example:
new DirectoryPartCatalog(@”.\extensions\”);
Run it and we get our new message!
STEP 3:
Cool, but we lost our old messages, and I kind of liked them too... Well, luckily, we have an aggregate part catalog that can take parts from several sources.
1: public void Run()
2: {
3: //SimpleHello hello = new SimpleHello();
4: //Message = hello.Message;
5: var catalog = new AggregatingComposablePartCatalog();
6: catalog.Catalogs.Add (new DirectoryPartCatalog(@"..\..\..\ExternalMessages\bin\Debug"));
7: catalog.Catalogs.Add (new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly()));
8: var container = new CompositionContainer(catalog.CreateResolver());
9: container.AddPart(this);
10: container.Compose();
Pretty cool, we now get all the messages!
输出为:
I am starting to get it ...
hello world!!
This is getting fun!
STEP 4:
Finally, just to bring up the point here... I created a bunch of different assemblies that exported Messages... All I need to do is point the catalog at them and go.
1: public void Run()
2: {
3: //SimpleHello hello = new SimpleHello();
4: //Message = hello.Message;
5: var catalog = new AggregatingComposablePartCatalog();
6: catalog.Catalogs.Add (new DirectoryPartCatalog(@"..\..\..\ExternalMessages\bin\Debug"));
7: catalog.Catalogs.Add(new DirectoryPartCatalog(@"..\..\..\ExtraMessages"));
8: catalog.Catalogs.Add (new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly()));
9: var container = new CompositionContainer(catalog.CreateResolver());
10: container.AddPart(this);
11: container.Compose();
12:
See where I added them in line 7.. Now just copy the assemblies into this directory and they become available for this program to use! Notice how I don't need to change any of the logic of the core program as I add more and more extensions.
输出看下图:
Taking MEF to the Next Level
Above I showed the simplest scenario... let's get a bit more powerful. If you pick apart the main program looking for tight coupling, that Console.WriteLine() will really stand out.. What if you want to log to a file, call a web service or print to HTML or WPF? Tightly coupling to the Console does not make that easy. How can we use the seperation of concerns principle and MEF to get rid of this tight coupling?
二、进行一点点扩展
STEP 1:
First, we need to define an interface that describes the contract for outputting strings. To ensure correct dependency management go ahead and create a new Library project called SharedLibrary, add this interface and reference this project from each of the other team projects.
1: namespace SharedLibrary
2: {
3: public interface IOutputString
4: {
5: void OutputStringToConsole(string value);
6: }
7: }
STEP 2:
Now, back in the main program we can factor out the Console.WriteLine ()...
1: class Program
2: {
3: [Import]
4: public IEnumerable<string> Messages { get; set; }
5:
6: [Import]
7: public IOutputString Out { get; set; }
8:
9: public void Run()
10: {
11: //SimpleHello hello = new SimpleHello();
12: //Message = hello.Message;
13: var catalog = new AggregatingComposablePartCatalog();
14: catalog.Catalogs.Add (new DirectoryPartCatalog(@"..\..\..\ExternalMessages\bin\Debug"));
15: catalog.Catalogs.Add(new DirectoryPartCatalog(@"..\..\..\ExtraMessages"));
16: catalog.Catalogs.Add (new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly()));
17: var container = new CompositionContainer(catalog.CreateResolver());
18: container.AddPart(this);
19: container.Compose();
20:
21: foreach (var s in Messages)
22: {
23: Out.OutputString(s);
24: }
25:
26:
27: Console.ReadKey();
28: }
In line 6-7 we define Out and in line 23 we change from Console.WriteLine() to Out.OutputString().
STEP 3:
Now in External Messages project add the following class
1: [Export(typeof(IOutputString))]
2: public class Class1 : IOutputString
3: {
4: public void OutputString(string value)
5: {
6: Console.WriteLine("Output=" + value);
7: }
8:
9:
Notice here we are explicitly saying the expert type to be that shared interface. Now when we run it we get:
- 大小: 129.1 KB
- 大小: 73.5 KB
- 大小: 63.2 KB
分享到:
相关推荐
MEF-报告:数字时代的个人隐私和对品牌的长期信任(英文)-2020.8-11页2020精品报告.pdf
使用MEF的即插即用服务 请参阅相关的博客文章
VS MEF(Visual Studio的托管可扩展性框架风格) 特征 您现有的MEF零件的新的,更快的主机 重用您已经在使用的MEF属性 ExportFactory支持创建具有范围有效期(即共享边界)的子容器 文献资料
bak-R-ız 组 这是我们的
这是目前metro ethernet 技术最权威的标准met,该文档是mef10.2
引导程序 MEF Autofac应用程序引导程序
MEF-Portfolio:民兵股票基金投资组合的数据可视化
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
2相关工作 2.1数据集VV [] SID []大声笑[] NPE [] MEF [] CID [] SCIE [] LIME [] DICM [] BIMEF [] ExDark [] MIT-Adobe FiveK [] 2.2论文 2020年Meng等人的GIA-Net:低光成像的全球信息感知网络。 [] [代码] Kwon...
VV,LIME,NPE系列,DICM,MEF 仅低照度图像而没有相应的高照度地面实况 感谢收集 SID 下载:+或 麻省理工学院-Adobe FiveK (仅使用专家C的输出) ExDARK 仅低光图像,没有相应的高光地面真相 哈哈 下载:或 赛斯 ...
MEF(Managed Extensibility Framework)简单实现
在WPF中MEF的应用,有简单实例和代码,MEF是主程序
MEF模块解耦,WCF Rest 扩展 基础示例