Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Managed Extensibility Framework (MEF) Mohamed Meligy Code: 02F01
Extensibility Extensions
 
<ul><li>DI is all about wiring up objects </li></ul><ul><li>Come on, that isn’t hard. We’ve been doing that for years! </l...
Web App Stock Quotes Authenticator Error Handler Logger Database This example was created by Jim Weirich in Ruby on his bl...
 
<ul><li>How does the StockQuotes find the Logger? </li></ul><ul><li>How does the Authenticator find the database? </li></u...
 
<ul><li>public class MyLocator : ILocator </li></ul><ul><li>{ </li></ul><ul><ul><li>protected Dictionary<Type, object> dic...
<ul><li>public class StockQuotes </li></ul><ul><li>{ </li></ul><ul><li>public StockQuotes(ILocator locator) </li></ul><ul>...
<ul><li>Classes are decoupled from explicit imlementation types </li></ul><ul><li>Easy to externalize the config </li></ul>
<ul><li>Everyone takes a dependency on the ILocator </li></ul><ul><li>Hard to store constants and other useful primitives ...
<ul><li>Gets rid of the dependency on the ILocator </li></ul><ul><li>Object is no longer responsible for finding its depen...
<ul><li>Write your objects the way you want </li></ul><ul><li>Setup the container </li></ul><ul><li>Ask the container for ...
DIContainer container = new DIContainer(); container.Register<ILogger, Logger>(); container.Register<IDatabase, Database>(...
 
 
<ul><li>To Singleton or Not to Singleton? </li></ul><ul><li>Nested Containers </li></ul><ul><li>Property Setter </li></ul>...
<ul><li>Spring.NET </li></ul><ul><li>Castle Windsor </li></ul><ul><li>StructureMap </li></ul><ul><li>Microsoft Unity (Ente...
 
 
<ul><li>Hard to enable, hard to create </li></ul><ul><li>Many extensibility models </li></ul><ul><li>Extensibility </li></...
Application ToolWindow  A ToolWindow  B Contacts Call Support Help File Edit View Tools Help
Application Extension A Extension B Need a  Toolwindow Got a Toolwindow Need Menus Got a Menu Need a MenuService Got a Men...
 
Windows And Menus With MEF public   interface   IToolWindow  { } public   interface   IMenuService  { }  public   interfac...
Windows And Menus With MEF [ Export ( typeof ( IToolWindow ))] public   class   SomeToolWindow  :  IToolWindow { [ Import ...
First, The Composable Part  Part  A
Parts Relate Through Contracts Part  B Part  A
Imports Are Contracts A Part Needs Part  B Import Import
Exports Are Contracts A Part Offers Export Export Part  B
The Container Is The Matchmaker Container
Container Queries Catalogs Catalog Container
Parts Can Load Lazily Part  B Part  A Export <A>
Parts Can Have Different Lifetimes Container Container Part A Singleton Part A Part A Part A Factory
 
 
<ul><li>Part of the framework </li></ul><ul><li>Microsoft will use it </li></ul><ul><li>Developers will use it </li></ul><...
<ul><li>http://www.codeplex.com/MEF </li></ul><ul><ul><li>MEF Home </li></ul></ul><ul><li>http://tinyurl.com/MEFGuide </li...
<ul><li>MEF Preview 4 Source Code & Samples </li></ul><ul><ul><li>http://www.codeplex.com/MEF/Release/ProjectReleases.aspx...
Upcoming SlideShare
Loading in …5
×

Managed Extensibility Framework (MEF)

4,744 views

Published on

Managed Extensibility Framework (MEF)

Managed Extensibility Framework (MEF)

  1. 1. Managed Extensibility Framework (MEF) Mohamed Meligy Code: 02F01
  2. 2. Extensibility Extensions
  3. 4. <ul><li>DI is all about wiring up objects </li></ul><ul><li>Come on, that isn’t hard. We’ve been doing that for years! </li></ul>
  4. 5. Web App Stock Quotes Authenticator Error Handler Logger Database This example was created by Jim Weirich in Ruby on his blog. See his original article http ://onestepback.org/index.cgi/Tech/Ruby/DependencyInjectionInRuby.rdoc
  5. 7. <ul><li>How does the StockQuotes find the Logger? </li></ul><ul><li>How does the Authenticator find the database? </li></ul><ul><li>Etc.? </li></ul><ul><li>Suppose you want to use a TestingLogger instead? Or a MockDatabase? </li></ul>
  6. 9. <ul><li>public class MyLocator : ILocator </li></ul><ul><li>{ </li></ul><ul><ul><li>protected Dictionary<Type, object> dict = </li></ul></ul><ul><ul><li>new Dictionary<Type,object>(); </li></ul></ul><ul><ul><li>public MyLocator() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>dict.Add(typeof(ILogger), new Logger()); </li></ul></ul><ul><ul><li>dict.Add(typeof(IErrorHandler), new ErrorHandler(this)); </li></ul></ul><ul><ul><li>dict.Add(typeof(IQuotes), new StockQuotes(this)); </li></ul></ul><ul><ul><li>dict.Add(typeof(IDatabase), new Database(this)); </li></ul></ul><ul><ul><li>dict.Add(typeof(IAuthenticator), new Authenticator(this)); </li></ul></ul><ul><ul><li>dict.Add(typeof(WebApp), new WebApp(this)); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  7. 10. <ul><li>public class StockQuotes </li></ul><ul><li>{ </li></ul><ul><li>public StockQuotes(ILocator locator) </li></ul><ul><li>{ </li></ul><ul><li>errorHandler = locator.Get<IErrorHandler>(); </li></ul><ul><li>logger = locator.Get<ILogger>(); </li></ul><ul><li>} </li></ul><ul><li>// More code here... </li></ul><ul><li>} </li></ul>
  8. 11. <ul><li>Classes are decoupled from explicit imlementation types </li></ul><ul><li>Easy to externalize the config </li></ul>
  9. 12. <ul><li>Everyone takes a dependency on the ILocator </li></ul><ul><li>Hard to store constants and other useful primitives </li></ul><ul><li>Creation order is still a problem </li></ul>
  10. 13. <ul><li>Gets rid of the dependency on the ILocator </li></ul><ul><li>Object is no longer responsible for finding its dependencies </li></ul><ul><li>The container does it for you </li></ul>
  11. 14. <ul><li>Write your objects the way you want </li></ul><ul><li>Setup the container </li></ul><ul><li>Ask the container for objects </li></ul><ul><li>The container creates objects for you and fulfills dependencies </li></ul>
  12. 15. DIContainer container = new DIContainer(); container.Register<ILogger, Logger>(); container.Register<IDatabase, Database>(); container.Register<IErrorHandler, ErrorHandler>(); container.Register<IQuotes, StockQuotes>(); container.Register<IAuthenticator, Authenticator>(); container.Register<IWebApp, WebApp>();
  13. 18. <ul><li>To Singleton or Not to Singleton? </li></ul><ul><li>Nested Containers </li></ul><ul><li>Property Setter </li></ul><ul><li>Object Lifetime </li></ul><ul><li>Method Invocation </li></ul><ul><li>Event Wire-up </li></ul><ul><li>Instrumentation </li></ul><ul><li>Method Interception via Dynamic Proxies </li></ul>
  14. 19. <ul><li>Spring.NET </li></ul><ul><li>Castle Windsor </li></ul><ul><li>StructureMap </li></ul><ul><li>Microsoft Unity (Enterprise Library) </li></ul><ul><li>MEF ????? </li></ul>
  15. 22. <ul><li>Hard to enable, hard to create </li></ul><ul><li>Many extensibility models </li></ul><ul><li>Extensibility </li></ul><ul><ul><li>Extending the extensions </li></ul></ul>2
  16. 23. Application ToolWindow A ToolWindow B Contacts Call Support Help File Edit View Tools Help
  17. 24. Application Extension A Extension B Need a Toolwindow Got a Toolwindow Need Menus Got a Menu Need a MenuService Got a MenuService
  18. 26. Windows And Menus With MEF public interface IToolWindow { } public interface IMenuService { }  public interface IMenu { }   public class Application { [ Import ] public IEnumerable < IToolWindow > ToolWindows { get ; set ; }   [ Import ] public IEnumerable < IMenu > Menus { get ; set ; } }   [ Export ( typeof ( IMenuService ))] public class MenuService : IMenuService { }
  19. 27. Windows And Menus With MEF [ Export ( typeof ( IToolWindow ))] public class SomeToolWindow : IToolWindow { [ Import ] public IMenuService MenuService { get ; set ; } }   [ Export ( typeof ( IMenu ))] public class SomeMenu : IMenu {}  
  20. 28. First, The Composable Part Part A
  21. 29. Parts Relate Through Contracts Part B Part A
  22. 30. Imports Are Contracts A Part Needs Part B Import Import
  23. 31. Exports Are Contracts A Part Offers Export Export Part B
  24. 32. The Container Is The Matchmaker Container
  25. 33. Container Queries Catalogs Catalog Container
  26. 34. Parts Can Load Lazily Part B Part A Export <A>
  27. 35. Parts Can Have Different Lifetimes Container Container Part A Singleton Part A Part A Part A Factory
  28. 38. <ul><li>Part of the framework </li></ul><ul><li>Microsoft will use it </li></ul><ul><li>Developers will use it </li></ul><ul><ul><li>Write add-ins </li></ul></ul><ul><ul><li>Create extensible apps </li></ul></ul><ul><ul><ul><li>Microsoft has not decided </li></ul></ul></ul>
  29. 39. <ul><li>http://www.codeplex.com/MEF </li></ul><ul><ul><li>MEF Home </li></ul></ul><ul><li>http://tinyurl.com/MEFGuide </li></ul><ul><ul><li>Programming Guide </li></ul></ul><ul><li>http://tinyurl.com/MEFDiscussions </li></ul><ul><ul><li>Discussions </li></ul></ul><ul><li>http://tinyurl.com/MEFBlogs </li></ul><ul><ul><li>Team Blogs </li></ul></ul><ul><li>http://channel9.msdn.com/pdc2008/TL33/ </li></ul><ul><ul><li>MEF @ PDC 2008 </li></ul></ul><ul><li>http://blogs.msdn.com/gblock/ </li></ul><ul><ul><li>Glenn Block, The MEF Guy </li></ul></ul>Code: 02F01 Eng . [email_address] . com weblogs . asp . net/meligy
  30. 40. <ul><li>MEF Preview 4 Source Code & Samples </li></ul><ul><ul><li>http://www.codeplex.com/MEF/Release/ProjectReleases.aspx?ReleaseId=22313 </li></ul></ul><ul><li>MEF Article Formatter Sample </li></ul><ul><ul><li>http://blogs.conchango.com/robhenry/archive/2008/11/11/managed-extensibility-framework-mef.aspx </li></ul></ul><ul><li>MEF Grid Sample </li></ul><ul><ul><li>http://blogs.msdn.com/dsplaisted/archive/2009/01/14/mefgrid-a-sample-mef-application.aspx </li></ul></ul>

×