The document discusses the Managed Extensibility Framework (MEF), a library developed by Microsoft that allows applications and components to be extended through extensions. MEF allows developers to easily discover and use extensions with their applications through a simple yet powerful composition model. Examples are provided of how MEF allows applications and components to be flexibly extended through external plug-ins and modules.
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
18. To Singleton or Not to Singleton? Nested Containers Property Setter Object Lifetime Method Invocation Event Wire-up Instrumentation Method Interception via Dynamic Proxies
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 {} Ìý
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