Extending the Enterprise with MEF

1,523 views

Published on

Building extensibility into your enterprise apps with the Managed Extensibility Framework

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

No Downloads
Views
Total views
1,523
On SlideShare
0
From Embeds
0
Number of Embeds
22
Actions
Shares
0
Downloads
19
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Extending the Enterprise with MEF

  1. 1. Extending the Enterprisewith MEFBuilding extensibility into your enterprise apps withthe Managed Extensibility FrameworkBrian Ritchie Jason GerardChief Architect Director of Product DevelopmentPaySpan, Inc. PaySpan, Inc.Twitter: @brian_ritchie Twitter: @thejasongerardEmail: brian.ritchie@gmail.com Email: jason.gerard@gmail.comBlog: http://weblog.asp.net/britchie Blog: http://jasongerard.wordpress.comWeb: http://www.dotnetpowered.com
  2. 2. Who are we?Brian Ritchie Jason Gerard» Chief Architect » Director of Development» Nearly 20 years of » Nearly 15 years of development experience development experience» Developing on .NET » Only fault is that he is only since 1.0 Beta 1. one man Contributor to Mono and other open source projects
  3. 3. Managed Extensibility FrameworkAgenda»What is MEF?»When to use MEF»Benefits of using MEF»Key Concepts»What’s new in .NET 4.5?»Demo!
  4. 4. What is MEF?The Managed Extensibility Framework or MEFis a library for creating lightweight, extensibleapplications.»It allows application developers to discover and useextensions with no configuration required. It letsextension developers easily encapsulate code andavoid fragile hard dependencies. MEF not only allowsextensions to be reused within applications, but acrossapplications as well.
  5. 5. When to use MEF?And when not to...
  6. 6. When to use MEF?» You are planning to star in a zombie movie» You weren’t born in Britain but want British teeth. NO! We’re talking about MEF not METH!!
  7. 7. When to use MEF?» Only if you are building an extensible UI with Silverlight or WPF applications NO!Building extensible applications is one of the key nonfunctional requirements inenterprise application development scenarios. .NET 4 for Enterprise Architects and Developers
  8. 8. When to use MEF?» When you need a general purpose Dependency Injection framework or an Inversion of Control Container NO! We are not aiming for MEF to be an all-purpose IoC. The best way to think about the IoC aspects of MEF is an implementation detail. We use IoC as a pattern because it is a great way to address the problems we are looking to solve. Glenn Block Microsoft
  9. 9. When to use MEF?» MEF is especially useful for large applications where injecting the dependencies between distant parts would get hard to manage as the size of the code base increases. YES! MEF is focused on extensibility. When you think of MEF look at it as an investment in taking our platform forward. Our future products and the platform will leverage MEF as a standard mechanism for adding extensibility. Imagine when you want to extend our platform in the future, you drop a DLL in the bin folder and you are done. The MEF enabled app lights up with the new extension. Thats the vision for MEF. Glenn Block Microsoft
  10. 10. Benefits of being a MEF user» Decomposes large systems into building blocks» Allows extensibility across teams» Turns your app into a platform
  11. 11. MEF Key ConceptsReady to get hooked on MEF?
  12. 12. MEF Key ConceptsExports & Imports»Exports expose contracts for functionalitythat can be replaced at runtime. Exports aredefined in your plugin libraries.»Imports are properties that consumeExported instances. Imports are defined inyour host application.
  13. 13. MEF Key Concepts» Exports Decorating a class with the Export attribute [Export(typeof(IPlugin)] [Export(typeof(IPlugin)] public class MyPlugin :: IPlugin public class MyPlugin IPlugin {{ }} The interface (IPlugin) should be in Contract Assembly that can be shared between the importer (host application) and the exporter (plugin).
  14. 14. MEF Key Concepts» Imports Decorating the host class with an ImportMany attribute public class Host public class Host {{ [ImportMany(typeof(IPlugin))] [ImportMany(typeof(IPlugin))] public IEnumberable<IPlugin> Plugins {{ get; set; }} public IEnumberable<IPlugin> Plugins get; set; ... ... }}The interface (IPlugin) should be in Contract Assembly that can be shared between the importer (host application) and the exporter (plugin).
  15. 15. MEF Key Concepts» Imports Exports can also define imports. For example, a plugin maybe dependent on a service from the host. [Export(typeof(IPlugin)] [Export(typeof(IPlugin)] public class MyPlugin :: IPlugin public class MyPlugin IPlugin {{ [Import] [Import] IService Dependency {{ get; set; }} IService Dependency get; set; }}
  16. 16. MEF Key ConceptsComposition»Composition binds the Export with an Importat runtime to deliver the functionality.»A Catalog hosts the exports that need to becomposed.»Types of catalogs: » Assembly Catalog » Directory Catalog » Aggregate Catalog » Type Catalog
  17. 17. MEF Key ConceptsComposition»Loading a catalog and binding the exports tothe imports: var catalog == new DirectoryCatalog(@".plugins"); var catalog new DirectoryCatalog(@".plugins"); var container == new CompositionContainer(catalog); var container new CompositionContainer(catalog); container.ComposeParts(host); container.ComposeParts(host);
  18. 18. MEF Key ConceptsComposition»The Plugins collection now containsinstances of all the exported types. public class Host public class Host {{ [ImportMany(typeof(IPlugin))] [ImportMany(typeof(IPlugin))] public IEnumberable<IPlugin> Plugins {{ get; set; }} public IEnumberable<IPlugin> Plugins get; set; ... ... }}
  19. 19. MEF Key ConceptsMetadata»Custom metadata can be attached to yourexported types to provide information to theimporter.»Simply add an ExportMetadata attributewhich accepts a key and a value: [ExportMetadata(“ID”, 1)] [ExportMetadata(“ID”, 1)] [Export(typeof(IPlugin)] [Export(typeof(IPlugin)] public class MyPlugin :: IPlugin public class MyPlugin IPlugin {{ }}
  20. 20. MEF Key ConceptsMetadata»In the importer, define an Interface to accessthe metadata. The property and data typemust match the name/value pair specified inthe ExportMetadata attribute. public interface PluginMetadata public interface PluginMetadata {{ int ID {{ get;set;} int ID get;set;} }}
  21. 21. MEF Key ConceptsMetadata»Next, define the import to also load the metadata.Access both via .NET lazy load class. public class Host public class Host {{ [ImportMany] [ImportMany] public IEnumerable<Lazy<IPlugin, IPluginMetadata>> Plugins {{ get; set; }} public IEnumerable<Lazy<IPlugin, IPluginMetadata>> Plugins get; set; void UsePlugins() void UsePlugins() {{ foreach (var pp in Plugins) foreach (var in Plugins) {{ var id == p.Metadata.ID; var id p.Metadata.ID; var plugin == p.Value; var plugin p.Value; }} }} }}
  22. 22. What’s new in .NET 4.5?» Support for generic types» Multiple scopes» Convention-based programming model that enables you to create parts based on rules rather than attributes. // Instead of using Export() attribute on Logger // Instead of using Export() attribute on Logger var registration == new var registration new RegistrationBuilder();registration.ForType<Logger>().Export<ILogger> RegistrationBuilder();registration.ForType<Logger>().Export<ILogger> (); (); // Apply to all classes derived from ILogger // Apply to all classes derived from ILogger registration.ForTypesDerivedFrom<ILogger>().Export<ILogger>(); registration.ForTypesDerivedFrom<ILogger>().Export<ILogger>();
  23. 23. Demo!Give me some MEF! https://github.com/jasongerard/MefExample
  24. 24. Extending the Enterprise with MEF Thanks for Coming!Brian Ritchie Jason GerardChief Architect Director of Product DevelopmentPaySpan, Inc. PaySpan, Inc.Twitter: @brian_ritchie Twitter: @thejasongerardEmail: brian.ritchie@gmail.com Email: jason.gerard@gmail.comBlog: http://weblog.asp.net/britchie Blog: http://jasongerard.wordpress.comWeb: http://www.dotnetpowered.com

×