Download presentation
Presentation is loading. Please wait.
1
Managed Extensibility Framework 托管可扩展性框架
1/13/2019 1:33 PM Managed Extensibility Framework 托管可扩展性框架 秦春林 © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
2
MEF Makes Extensibility Fabulous
3
Glenn Block --MEF产品经理
6
一般可扩展应用程序架构 框架设计者 App Framework Third Model Navigation Model
Main Work Model 导航模块设计者 模块设计者 第三方插件设计者
10
RIA Extensions, add-ins, and plugins oh my!
11
Extensibility implants
12
Turning fat apps into slimmer apps
16
Import Export Need Got
17
An Application is built of parts.
MEF Basics… An Application is built of parts.
19
Demo
20
MEF Basics… Export it. Import it. Compose it.
21
Widget1 Export it. Export 导出部件必须 包含公共 无参构造函数
[Export(typeof(UserControl))] public class Widget1 : UserControl { public MainPage(){ InitializeComponent(); } public string Message { get{return(string) Button.Content;} set{Button.Content=value;} 导出部件必须 包含公共 无参构造函数 Widget1 Export
22
Widget1 Import it. Import String [Export(typeof(UserControl))]
public class Widget1 : UserControl { [Import] public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } Widget1 String Import
23
Widget1 Import it. Import “HelloMEF.Message”
[Export(typeof(UserControl))] public class Widget1 : UserControl { [Import(“HelloMEF.Message”)] public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } Widget1 “HelloMEF.Message” Import
24
Main Page Import it. ImportMany UserControl
[Export(typeof(UserControl))] public class MainPage: UserControl { [ImportMany(typeof(UserControl))] public IEnumerable<UserControl> { get;set; } Main Page UserControl ImportMany
25
PartIntializer: “Compose yourself”
Compose it. PartIntializer: “Compose yourself” public MainPage() { InitializeComponent(); var catalog = new DirectoryCatalog("."); var container = new CompositionContainer(catalog); container.ComposeParts(this); } Main Page Compose
28
Be different – Use metadata
29
Where does the widget go?
30
Widget1 Export it - Metadata Export Put me in the top UserControl
[ExportMetadata(“Location”,Location.Top)] [Export(typeof(UserControl))] public class Widget1 : UserControl { public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } Widget1 Put me in the top UserControl Export
31
Main Page Import it - Metadata ImportMany UserControl
[Export(typeof(UserControl))] public class MainPage: UserControl { [ImportMany(typeof(UserControl))] public IEnumerable<Lazy<UserControl, IWidgetMetadata> get;set; } Main Page UserControl ImportMany
32
Have it your way! – Custom Exports
33
Widget1 Export it - Metadata Export Put me in the top UserControl
[ExportMetadata(“Location”,Location.Top)] [Export(typeof(UserControl))] public class Widget1 : UserControl { public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } Widget1 Put me in the top UserControl Export
34
Customize it – Custom exports
[Widget(Location=Location.Top)] public class Widget1 : UserControl { public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } Widget1 Put me in the top UserControl Export
35
Demo
36
1/13/2019 1:33 PM MEF and Prism © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
37
Introducing MEF-Prism integration
Goal : Enable an existing Prism application to be further extended through MEF Enabled MEF modules to be added dynamically on the fly. Enable MEF discovered components to be registered in IoC container (Such as Unity).
38
Introducing MEF-Prism integration
Prism application starts and loads up known modules. Module Loader
39
Introducing MEF-Prism integration
Prism application can load unknown plugins, by using MEF as an abstraction layer. 1. Plugins are located and added to a MEF catalog. Server
40
Introducing MEF-Prism integration
Prism application can load unknown plugins, by using MEF as an abstraction layer. 2. Plugins register dependencies with current IoC container. Server
41
Introducing MEF-Prism integration
Prism application can load unknown plugins, by using MEF as an abstraction layer. 3. Plugins are activated. Dependencies are injected. Server
42
Where can I get it? MEF ships in Silverlight 4.0
Dynamic XAP support in SL 4.0 Toolkit Silverlight 3.0 support on Codeplex MEF source ships under MSPL Learn more: Silverlight.net MEF.Codeplex.com MEFContrib.Codeplex.com – Prism/MEF blogs.msdn.com/gblock
43
MEF Cross-platform(s)
1/13/2019 1:33 PM MEF Cross-platform(s) © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
44
深入MEF
46
MEF的几个核心概念 可组合的部件 导出 导入 约定 组合
47
使用MEF的一般步骤 添加需要容器创建的约定的导入 创建MEF用于发现部件的目录 创建组合部件实例的容器
通过对容器调用Composeparts方法并传入具有导入的实例,来进行组合
48
.NET Composition Architecture
托管层 Hosting 编程模型 Attributed Model 基元层 Primitives .NET Composition Architecture
49
基元层-生成部件和编程模型的位置 System.ComponentModel.Composition.Primitives
基元层是MEF的核心和基础。基元定义部件的抽象表现形式 在这个架构中开发者可以使用不同的编程模型来使用基元,MEF提供一种基于特性编程的编程模型
51
特性化编程模型 Attributed Programming Model
System.ComponentModel.Composition 编程模型定义部件,导入和导出 特性化编程模型使用特性(Attribute)根据契约将一般的.NET对象标记为部件—特性化部件 编程模型配合托管层进行查找部件和组合部件
52
托管层-进行组合的位置 System.ComponentModel.Composition.Hosting
托管层提供了很大的灵活性,可配置性和可扩展性 目录提供可用导出和导出的部件定义。它们是MEF中用于发现的主要单元 TypeCatalog AssemblyCatalog DirectoryCatalog 目录从ComposablepartCatalog继承,并且是MEF的扩展点。自定义目录有多种用途: 从提供全新的编程模型到封装和赛选现有目录
54
通过构造函数导入 也成为构造函数注入 使用导入构造函数时,MEF会假设所有参数都是导入,从而不必使用Import特性
构造函数参数不支持重新组合
55
控制部件创建策略 默认情况下,容器中的所有部件实例都是单例,因为由在容器中导入它们的所有部件共享。
如果有时每个导入程序都要获取自己的实例,可以设置创建策略 CreationPolicy.Shared CreationPolicy.NonShared CreationPolocy.Any
57
导入延迟导出和访问元数据 System.Lazy<T> 使用该API可延迟实例的实例化,直至访问Lazy的Value属性
MEF使用Lazy<T,TMetadata>进一步扩展Lazy<T>,以允许在不实例化基础导出的情况下访问导出元数据 Tmetadata是元数据的视图类型。元数据视图是接口,用于定义对应所导出元数据中的键的只读属性。访问元数据属性时,MEF将动态实现Tmetadata,且将基于导出提供的元数据来设置值
58
重新组合 此功能允许部件在系统中出现新的匹配导出时自动更新其导入。
重新组合在某些方案中十分有用,例如从远程服务器下载部件时,可以启动多个可选视图的下载 AllowRecomposition=true 进行组合时,导入集合将立即替换为包含一组更新过的新的集合 有两个目录支持重新组合: DirectoryCatalog通过调用其Refresh方法来重新组合 AggregateCatalog使用Catalogs集合属性向该目录添加目录启动重新组合
59
MEF的世界 小到属性值的注入 构造函数参数的注入 服务的注入 大到模块的集成 第三方插件扩展 MEF提供的是一个可扩展性的框架
更是一种设计的指导 模块化,实现和接口分离,更好的扩展性,开放性
60
你还想到了什么?
61
MEF资源 MEF项目主页: 在.NET4中使用托管可扩展性框架构建可组合的应用程序
1/13/2019 1:33 PM MEF资源 MEF项目主页: 在.NET4中使用托管可扩展性框架构建可组合的应用程序 Managed Extensibility Framework: Overview Building Extensible RIAs with the Managed Extensibility Framework MEF程序设计指南(博客园): © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
62
Thanks
Similar presentations