ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Managed Extensibility Framework (MEF) Mohamed Meligy Code: 02F01
Extensibility Extensions
Ìý
DI is all about wiring up objects Come on, that isn’t hard. We’ve been doing that for years!
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
Ìý
How does the StockQuotes find the Logger? How does the Authenticator find the database? Etc.? Suppose you want to use a TestingLogger instead? Or a MockDatabase?
Ìý
public class MyLocator : ILocator { protected Dictionary<Type, object> dict =  new Dictionary<Type,object>(); public MyLocator() { dict.Add(typeof(ILogger), new Logger()); dict.Add(typeof(IErrorHandler),  new ErrorHandler(this)); dict.Add(typeof(IQuotes), new StockQuotes(this)); dict.Add(typeof(IDatabase), new Database(this)); dict.Add(typeof(IAuthenticator), new Authenticator(this)); dict.Add(typeof(WebApp), new WebApp(this)); } }
public class StockQuotes { public StockQuotes(ILocator locator) { errorHandler =  locator.Get<IErrorHandler>(); logger = locator.Get<ILogger>(); } // More code here... }
Classes are decoupled from explicit imlementation types Easy to externalize the config
Everyone takes a dependency on the ILocator Hard to store constants and other useful primitives Creation order is still a problem
Gets rid of the dependency on the ILocator Object is no longer responsible for finding its dependencies The container does it for you
Write your objects the way you want Setup the container Ask the container for objects The container creates objects for you and fulfills dependencies
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>();
Ìý
Ìý
To Singleton or Not to Singleton? Nested Containers Property Setter Object Lifetime Method Invocation Event Wire-up Instrumentation Method Interception via Dynamic Proxies
Spring.NET Castle Windsor StructureMap Microsoft Unity (Enterprise Library) MEF ?????
Ìý
Ìý
Hard to enable, hard to create Many extensibility models Extensibility Extending the extensions 2
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 MenuService
Ìý
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  { }
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 {} Ìý
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
Ìý
Ìý
Part of the framework Microsoft will use it Developers will use it Write add-ins Create extensible apps Microsoft has not decided
http://www.codeplex.com/MEF MEF Home http://tinyurl.com/MEFGuide Programming Guide http://tinyurl.com/MEFDiscussions Discussions http://tinyurl.com/MEFBlogs Team Blogs http://channel9.msdn.com/pdc2008/TL33/ MEF @ PDC 2008 http://blogs.msdn.com/gblock/ Glenn Block, The MEF Guy Code: 02F01 Eng . [email_address] . com weblogs . asp . net/meligy
MEF Preview 4 Source Code & Samples http://www.codeplex.com/MEF/Release/ProjectReleases.aspx?ReleaseId=22313 MEF Article Formatter Sample http://blogs.conchango.com/robhenry/archive/2008/11/11/managed-extensibility-framework-mef.aspx MEF Grid Sample http://blogs.msdn.com/dsplaisted/archive/2009/01/14/mefgrid-a-sample-mef-application.aspx

More Related Content

Managed Extensibility Framework (MEF)

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