Managed Extensibility Framework (MEF)
-
Upload
mohamed-meligy -
Category
Documents
-
view
4.206 -
download
1
description
Transcript of Managed Extensibility Framework (MEF)
Managed Extensibility Framework (MEF)
Mohamed Meligy
Code: 02F01Code: 02F01
Extensibility
Extensions
• DI is all about wiring up objects
• Come on, that isn’t hard. We’ve been doing that for years!
Web AppWeb App
Stock QuotesStock Quotes AuthenticatorAuthenticator
Error HandlerError Handler LoggerLogger DatabaseDatabase
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 Dictionar
y<Type, object> dict =
new
Dictionary<Type,object>();
public MyLocator()
{dict.Add(typeof(ILo
gger), 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(th
is));
}}
public class StockQuotes{public StockQuotes(ILocator locator){errorHandler = locator.Get<IErrorHandler>();logger = locator.Get<ILogger>();}
// More code here...}
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 AToolWindow A
ToolWindow BToolWindow B
Contacts Call Support Help
File Edit View Tools Help
ApplicationApplication
Extension A
Extension A
Extension B
Extension B
Need a Toolwindow
Need a Toolwindow
Got a Toolwindow
Got a Toolwindow
Need MenusNeed
Menus
Got aMenuGot aMenu
Need a MenuService
Need a MenuService
Got 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 { }
[Export(typeof(IToolWindow))]public class SomeToolWindow : IToolWindow{ [Import] public IMenuService MenuService { get; set; }} [Export(typeof(IMenu))]public class SomeMenu : IMenu{}
Windows And Menus With MEF
First, The Composable Part
Part A
Parts Relate Through Contracts
Part B
Part A
Imports Are Contracts A Part Needs
Part B
ImportImport
ImportImport
ExportExport
ExportExport
Exports Are Contracts A Part Offers
Part B
The Container Is The Matchmaker
Container
CatalogCatalog
Container Queries Catalogs
Container
Parts Can Load Lazily
Part B
Part A
Export<A>Export<A>
ContainerContainer
Parts Can Have Different Lifetimes
Part A
SingletonSingleton
Part A Part A Part A
FactoryFactory
• 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
[email protected]/meligy
Code: 02F01Code: 02F01
• 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