三、引申扩展
STEP 1:
To make things more interesting, lets add another implementation of IOutputString that is a bit more creative.
1: [Export(typeof(IOutputString))]
2: public class ReverseOutputter : IOutputString
3: {
4:
5: public void OutputString(string value)
6: {
7: foreach (var s in value.Split().Reverse())
8: {
9: Console.ForegroundColor = (ConsoleColor)(s.Length % 10);
10: Console.Write(s + " ");
11: }
12: Console.WriteLine();
13: }
14: }
Just running this now would give us an error right, because we told MEF we wanted exactly one IOutputString... if we change our code to work with multiple we get more fun!
STEP 2:
In line 7 we changed to request a set of IOutputStrings and in line 19 we changed to loop through all the output devices.
1: class Program
2: {
3: [Import]
4: public IEnumerable<string> Messages { get; set; }
5:
6: [Import]
7: public IEnumerable<IOutputString> OutputSet { get; set; }
8:
9: public void Run()
10: {
11: var catalog = new AggregatingComposablePartCatalog();
12: catalog.Catalogs.Add (new DirectoryPartCatalog(@"..\..\..\ExternalMessages\bin\Debug"));
13: catalog.Catalogs.Add(new DirectoryPartCatalog(@"..\..\..\ExtraMessages"));
14: catalog.Catalogs.Add (new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly()));
15: var container = new CompositionContainer(catalog.CreateResolver());
16: container.AddPart(this);
17: container.Compose();
18:
19: foreach (var Out in OutputSet)
20: {
21: foreach (var s in Messages)
22: {
23: Out.OutputString(s);
24: }
25: }
26:
27:
28: Console.ReadKey();
29: }
Now when we run it we get all our messages in all our outputs..
输出图示:
STEP 3:
Now the only real logic in our main is the nested foreach loops. We may very well like to change that in the future as well... so let's see if we can abstract that out using exactly the same techniques we have talked about already.
1: class Program
2: {
3: [Import]
4: public IEnumerable<string> Messages { get; set; }
5:
6: [Import]
7: public IEnumerable<IOutputString> OutputSet { get; set; }
8:
9: [Import("OutputMessages")]
10: public Action<IEnumerable<IOutputString>, IEnumerable<string>> OutputMessages { get; set; }
11:
12: public void Run()
13: {
14: var catalog = new AggregatingComposablePartCatalog();
15: catalog.Catalogs.Add (new DirectoryPartCatalog(@"..\..\..\ExternalMessages\bin\Debug"));
16: catalog.Catalogs.Add(new DirectoryPartCatalog(@"..\..\..\ExtraMessages"));
17: catalog.Catalogs.Add (new AttributedAssemblyPartCatalog(Assembly.GetExecutingAssembly()));
18: var container = new CompositionContainer(catalog.CreateResolver());
19: container.AddPart(this);
20: container.Compose();
21:
22:
23: OutputMessages(OutputSet, Messages);
24:
25: }
First, in line 9-10 we define an Action that can do the output... and in line 23 we remove our nested foreach loops and replace it with a call to this method. Now all we have in our Main is effectively wiring up components. Very loosely coupled!
STEP 4:
Now let's add an option for how the messages are outputted. It would be very easy to define a config file option for selecting these and returning the correct one. Notice that MEF knows that this method follows the contract of the Action delegate above, so it does all the wireup for us.
1: [Export("OutputMessages")]
2: public void OutputByMessage(IEnumerable<IOutputString> outputSet, IEnumerable<string> messages)
3: {
4: foreach (var s in messages)
5: {
6: foreach (var Out in outputSet)
7: {
8: Out.OutputString(s);
9: }
10: }
11: Console.ReadKey();
12: }
I would love to hear what you think? Can you think of usage for MEF in your applications? I'd also love for you to grab the final sample and see how many different ways you can extend it.. Enjoy!
- 大小: 90.5 KB
分享到:
相关推荐
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
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 扩展 基础示例