SlideShare 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

Similar to Managed Extensibility Framework (MEF) (20)

PPT
Extending the Enterprise with MEF
Brian Ritchie
 
PPTX
Building extensible application using MEF
Ronak Thakkar
 
KEY
MEFilicious Applications
buildmaster
 
PDF
Managed Extensibility Framework
Volodymyr Voytyshyn
 
PPTX
Managed Extensibility Framework or Effective Development of Scalable Applicat...
SoftServe
 
PPTX
MEF in Silverlight 4 - a guided tour
mark mann
 
PPTX
MEF Deep Dive by Piotr Wlodek
infusiondev
 
PPTX
MEF
Yuriy Seniuk
 
PDF
Resolve dependency of dependencies using Inversion of Control and dependency ...
Akhil Mittal
 
PPTX
When MEF meets Silverlight
Amr Elsehemy
 
PPTX
Creating extendable applications using MEF
Ben Hall
 
PPT
Mef 12 step program
JamesEggers
 
DOCX
The managed extensibility framework
SpencersSlides
 
PPT
Mef with meta data and lazy loading
Krunal Trivedi
 
PPTX
Microsoft Managed Extensibility Framework
Binu Bhasuran
 
PDF
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Akhil Mittal
 
PPTX
Fun with ASP.NET MVC 3, MEF and NuGet
Maarten Balliauw
 
PDF
Beyond MVC: from Model to Domain
Jeremy Cook
 
PDF
Smart Client Development
Tamir Khason
 
PPTX
Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"
GlobalLogic Ukraine
 
Extending the Enterprise with MEF
Brian Ritchie
 
Building extensible application using MEF
Ronak Thakkar
 
MEFilicious Applications
buildmaster
 
Managed Extensibility Framework
Volodymyr Voytyshyn
 
Managed Extensibility Framework or Effective Development of Scalable Applicat...
SoftServe
 
MEF in Silverlight 4 - a guided tour
mark mann
 
MEF Deep Dive by Piotr Wlodek
infusiondev
 
Resolve dependency of dependencies using Inversion of Control and dependency ...
Akhil Mittal
 
When MEF meets Silverlight
Amr Elsehemy
 
Creating extendable applications using MEF
Ben Hall
 
Mef 12 step program
JamesEggers
 
The managed extensibility framework
SpencersSlides
 
Mef with meta data and lazy loading
Krunal Trivedi
 
Microsoft Managed Extensibility Framework
Binu Bhasuran
 
Enterprise Level Application Architecture with Web APIs using Entity Framewor...
Akhil Mittal
 
Fun with ASP.NET MVC 3, MEF and NuGet
Maarten Balliauw
 
Beyond MVC: from Model to Domain
Jeremy Cook
 
Smart Client Development
Tamir Khason
 
Java TechTalk "Spring boot made life easier with Kubernetes and Microservices"
GlobalLogic Ukraine
 

More from Mohamed Meligy (8)

PPTX
NDC Sydney 2018 Ngrx
Mohamed Meligy
 
PPT
DDD Sydney 20111 Razor Session
Mohamed Meligy
 
PPT
Twitter OAuth With C#/.NET Code
Mohamed Meligy
 
PPTX
Using jQuery To Survive In ASP.NET Webforms World
Mohamed Meligy
 
PPT
Framework Design Guidelines
Mohamed Meligy
 
PDF
Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC
Mohamed Meligy
 
PDF
Design Patterns Via C# 3.0
Mohamed Meligy
 
PPS
Scrum For Developers
Mohamed Meligy
 
NDC Sydney 2018 Ngrx
Mohamed Meligy
 
DDD Sydney 20111 Razor Session
Mohamed Meligy
 
Twitter OAuth With C#/.NET Code
Mohamed Meligy
 
Using jQuery To Survive In ASP.NET Webforms World
Mohamed Meligy
 
Framework Design Guidelines
Mohamed Meligy
 
Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC
Mohamed Meligy
 
Design Patterns Via C# 3.0
Mohamed Meligy
 
Scrum For Developers
Mohamed Meligy
 
Ad

Managed Extensibility Framework (MEF)

  • 1. Managed Extensibility Framework (MEF) Mohamed Meligy Code: 02F01
  • 3.  
  • 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
  • 6.  
  • 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?
  • 8.  
  • 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