MANAGED EXTENSIBILITY FRAMEWORK (MEF)
LarryNung
AGENDA
What is MEF
Architecture & Concept
Requirement
How to use
Demo
Reference
Q & A
WHAT IS MEF
WHAT IS MEF
 A library for creating lightweight, extensible applications.
 Discover and use extensions with no configura...
ARCHITECTURE & CONCEPT
ARCHITECTURE & CONCEPT
Import
Export
Part
CompositionContainer
Catalog
ARCHITECTURE & CONCEPT
REQUIREMENT
REQUIREMENT
 Assembly
 .NET 3.5+
 Silverlight 3.0+
 Reference
 System.ComponentModel.Composition.dll
 Namespace
 us...
HOW TO USE
HOW TO USE
HOW TO USE
Step3: Compose Import & Export
Step2: Decorate ExportAttribute
Step1: Decorate ImportAttribute
COMPOSE PARTS
private void Compose()
{
var catalog = new TypeCatalog(this.GetType());
var container = new CompositionConta...
COMPOSE PARTS
private void Compose()
{
var
catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecu
tingAssembly(...
COMPOSE PARTS
private void Compose()
{
var catalog = new DirectoryCatalog(Environment.CurrentDirectory,
"*.dll");
var cont...
COMPOSE PARTS
private void Compose()
{
var catalog = new ApplicationCatalog();
var container = new CompositionContainer(ca...
COMPOSE PARTS
private void Compose()
{
var
assemblyCatalog = new AssemblyCatalog(System.Reflection.Assembly.GetEx
ecutingA...
IMPORT & EXPORT
[Import]
string message;
...
[Export]
public string Message
{
get { return "Test..."; }
}
...
IMPORT & EXPORT
[Import(“Message”)]
string message;
...
[Export(“Message”)]
public string Message
{
get { return "Test..."...
IMPORT & EXPORT
[InheritedExport(typeof(IRule))]
public interface IRule
public class RuleOne : IRule {…}
public class Rule...
IMPORT & EXPORT
public class MyClass
{
[ImportingConstructor]
public MyClass([Import(typeof(I
Rule))] IRule myRule)
{
_myR...
DEMO
DEMO
namespace PlugIn.Core
{
public interface IHost {
IEnumerable<IModule> Modules { get; set; }
}
}
DEMO
namespace PlugIn.Core
{
[InheritedExport] public interface IModule
{
String Name { get; }
IHost Host { get; set; }
vo...
DEMO
namespace Host {
public partial class MainForm : Form, IHost {
...
[ImportMany] public IEnumerable<IModule> Modules {...
DEMO
REFERENCE
27
REFERENCE
 [C#]MEF開發系列 - Managed Extensibility Framework(MEF)的概念與
簡介 - Level Up- 點部落
 http://www.dotblogs.com.tw/larrynu...
REFERENCE
 Managed Extensibility Framework - Building Composable Apps
in .NET 4 with the Managed Extensibility Framework
...
Q&A
30
QUESTION & ANSWER
31
Upcoming SlideShare
Loading in …5
×

Managed extensibility framework

1,022 views

Published on

Published in: Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,022
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
31
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Managed Extensibility Framework 或 MEF 是用於建立輕量型可擴充應用程式的程式庫。 它可讓應用程式開發人員無需任何設定,即可探索並使用擴充功能。 它也可讓擴充功能開發人員輕易地封裝程式碼並避免出現不牢固的硬式相依性。 透過 MEF,不僅可在應用程式內重複使用擴充功能,也可跨應用程式重複使用擴充功能。

    主要側重在 探索性、擴充性、與可移植性
  • Composable part 是composable unit,也就是最小的組成單位,可以提供服務給其他組件使用(擴充功能)或是操作其他組件提供的服務(擴充點)。
    Export 是指提供的服務,也就是我們所說的擴充功能。透過ExportAttribute來設定。
    Import 是指所使用的服務,也就是我們所說的擴充點。透過ImportAttribute來設定。
    Composition Container 是組合容器,會去mapping import跟export、組合Composable part。
    Catalog 主要是用來探索Composable part用的,可從型別、組件 (Assembly) 或目錄 (Directory)探索到Composable part。
  • Managed extensibility framework

    1. 1. MANAGED EXTENSIBILITY FRAMEWORK (MEF) LarryNung
    2. 2. AGENDA What is MEF Architecture & Concept Requirement How to use Demo Reference Q & A
    3. 3. WHAT IS MEF
    4. 4. WHAT IS MEF  A library for creating lightweight, extensible applications.  Discover and use extensions with no configuration required.  Easily encapsulate code.  Avoid fragile hard dependencies.
    5. 5. ARCHITECTURE & CONCEPT
    6. 6. ARCHITECTURE & CONCEPT Import Export Part CompositionContainer Catalog
    7. 7. ARCHITECTURE & CONCEPT
    8. 8. REQUIREMENT
    9. 9. REQUIREMENT  Assembly  .NET 3.5+  Silverlight 3.0+  Reference  System.ComponentModel.Composition.dll  Namespace  using System.ComponentModel.Composition;
    10. 10. HOW TO USE
    11. 11. HOW TO USE
    12. 12. HOW TO USE Step3: Compose Import & Export Step2: Decorate ExportAttribute Step1: Decorate ImportAttribute
    13. 13. COMPOSE PARTS private void Compose() { var catalog = new TypeCatalog(this.GetType()); var container = new CompositionContainer(catalog); container.ComposeParts(this); }
    14. 14. COMPOSE PARTS private void Compose() { var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecu tingAssembly()); var container = new CompositionContainer(catalog); container.ComposeParts(this); }
    15. 15. COMPOSE PARTS private void Compose() { var catalog = new DirectoryCatalog(Environment.CurrentDirectory, "*.dll"); var container = new CompositionContainer(catalog); container.ComposeParts(this); }
    16. 16. COMPOSE PARTS private void Compose() { var catalog = new ApplicationCatalog(); var container = new CompositionContainer(catalog); container.ComposeParts(this); }
    17. 17. COMPOSE PARTS private void Compose() { var assemblyCatalog = new AssemblyCatalog(System.Reflection.Assembly.GetEx ecutingAssembly()); var directoryCatalog = new DirectoryCatalog(Environment.CurrentDirectory, "*.dll"); var catalog = new AggregateCatalog (assemblyCatalog, directoryCatalog); var container = new CompositionContainer(catalog); container.ComposeParts(this); }
    18. 18. IMPORT & EXPORT [Import] string message; ... [Export] public string Message { get { return "Test..."; } } ...
    19. 19. IMPORT & EXPORT [Import(“Message”)] string message; ... [Export(“Message”)] public string Message { get { return "Test..."; } } … [Export(typeof(IConfigInfo))] public class MyClass : IConfigInfo {… } [Import(typeof(IConfigInfo))] object _port1; [Import(typeof(IConfigInfo))] IConfigInfo _port1; [Import(typeof(IConfigInfo))] Lazy<IConfigInfo> _port1;
    20. 20. IMPORT & EXPORT [InheritedExport(typeof(IRule))] public interface IRule public class RuleOne : IRule {…} public class RuleTwo : IRule {…} [ImportMany(typeof(IRule))] List<IRule> _rules; [ImportMany(typeof(IRule))] IRule[] _rules; [ImportMany(typeof(IRule))] IEnumerable<IRule> _rules; [ImportMany(typeof(IRule))] IEnumerable<Lazy<IRule>> _rule s;
    21. 21. IMPORT & EXPORT public class MyClass { [ImportingConstructor] public MyClass([Import(typeof(I Rule))] IRule myRule) { _myRule = myRule; } } [Export, PartCreationPolicy(CreationPolicy. Any)] public class MyClass {…} [Import] MyClass _port1; [Import] MyClass _port2;
    22. 22. DEMO
    23. 23. DEMO namespace PlugIn.Core { public interface IHost { IEnumerable<IModule> Modules { get; set; } } }
    24. 24. DEMO namespace PlugIn.Core { [InheritedExport] public interface IModule { String Name { get; } IHost Host { get; set; } void Execute(); } }
    25. 25. DEMO namespace Host { public partial class MainForm : Form, IHost { ... [ImportMany] public IEnumerable<IModule> Modules { get; set; } public MainForm() { InitializeComponent(); var catalog = new DirectoryCatalog(Environment.CurrentDirectory, "*.dll"); var container = new CompositionContainer(catalog); container.ComposeParts(this); foreach (IModule module in Modules) { module.Host = this; 模組MToolStripMenuItem.DropDownItems.Add(module.Name, null, Module_Click).Tag = module; } } ... } }
    26. 26. DEMO
    27. 27. REFERENCE 27
    28. 28. REFERENCE  [C#]MEF開發系列 - Managed Extensibility Framework(MEF)的概念與 簡介 - Level Up- 點部落  http://www.dotblogs.com.tw/larrynung/archive/2012/12/11/85576.aspx?fid=72 390  Managed Extensibility Framework - Home  http://mef.codeplex.com/  Managed Extensibility Framework - Wikipedia, the free encyclopedia  http://en.wikipedia.org/wiki/Managed_Extensibility_Framework
    29. 29. REFERENCE  Managed Extensibility Framework - Building Composable Apps in .NET 4 with the Managed Extensibility Framework  https://msdn.microsoft.com/en-us/magazine/ee291628.aspx  [VS2010 Online]Managed Extensibility Framework in Visual Studio 2010 -- (1) - Response.Write("Hello LOLOTA")- 點部落  http://www.dotblogs.com.tw/lolota/archive/2010/03/03/13860.aspx  Managed Extensibility Framework (MEF)  https://msdn.microsoft.com/zh-tw/library/dd460648(v=vs.110).aspx
    30. 30. Q&A 30
    31. 31. QUESTION & ANSWER 31

    ×