Visual Studio 2010 and.NET Framework 4 Training Workshop
Presentation Outline (hidden slide): Technical Level: 300 Intended Audience: Developers & Architects Objectives (what do you want the audience to take away): Understand why and where MEF would be used Understand how MEF is used Presentation Outline: What is extensibility? Where does MEF fit? MEF Basics (Import/Export/Container/Catalogs) MEF Intermediate Topics Lifetime Scoping
A Lap Around Managed Extensibility Framework Name Title Organization
Objectives Understand why and where MEF would be used Understand how MEF is used
A MEF-ified Application
Managed Extensibility Framework? The Managed Extensibility Framework (MEF) is a new library in the.NET Framework that enables greater reuse of applications and components. Using MEF,.NET applications can make the shift from being statically compiled to dynamically composed
What is “Extensibility”? In software engineering, extensibility is a systemic measure of the ability to extend a system and the level of effort required to implement the extension.
Open/Closed Principle Software entities should be open for extension, but closed for modification.
KnownUnknown VS.
MEF ABCs
Application built of parts…
Using MEF is as simple as Import and Export
Part, enter stage left… Part A public class SimpleMortgageCalculator : IMortgageCalculator { public ILogger Logger { get; set; } public float Calculate() { Logger.Log("Calculating Mortgage"); return...; }
Imports are Contracts a part needs… Part A public class SimpleMortgageCalculator : IMortgageCalculator { [Import(typeof(ILogger))] public ILogger Logger { get; set; } public float Calculate() { Logger.Log("Calculating Mortgage"); return...; } Import
Exports are Contracts a part offers… Part A [Export(typeof(IMortgageCalculator))] public class SimpleMortgageCalculator : IMortgageCalculator { [Import(typeof(ILogger))] public ILogger Logger { get; set; } public float Calculate() { Logger.Log("Calculating Mortgage"); return...; } Export
Container is the Match Maker Container
Catalogs provide Parts Container Catalog
Catalogs provide Parts Container Catalog TypeCatalog AssemblyCatalog DirectoryCatalog AggregatingCatalog
Hello, MEF!
Parts can be lazy… Part B Export Export [Import(typeof(ILogger))] public ILogger Export Logger { get; set; }
Lifetime Container Part A Part B [CompositionOptions(CreationPolicy=CreationPolicy.NonShared)] [CompositionOptions(CreationPolicy=CreationPolicy.Shared)] Shared Non-Shared On Export [Import(RequiredCreationPolicy=CreationPolicy.NonShared)] [Import(RequiredCreationPolicy=CreationPolicy.Shared)] On Import
Export Defaults Custom Logger Default Logger Export Order Manager Import [Import(typeof(ILogger)] public MortgageCalculator (ILogger logger) { }
Scoping Child Container Parent Container Catalog
ImperativeDeclarative What How The importance of declarative
The importance of discovery Think of a Phonebook…