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
Understand importance of extensibility to software Understand when and how MEF is used Relation to other existing technologies Your feedback Objectives
The Problem… Original Software Development Software Maintenance
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 Managed Extensibility Framework?
2) To put things together to form a whole com·pose [ kəm pṓz ] MSN Encarta
Open/Closed Principle Software entities should be open for extension, but closed for modification.
Known vs. Unknown
MEF Basics… An Application is built of parts.
MEF Basics… Export it. Import it. Compose it.
Part, enter stage left… Part A public class SimpleMortgageCalculator : IMortgageCalculator { public ILogger Logger { get; set; } public float Calculate() { Logger.Log("Calculating Mortgage"); return...; }
Export it… Part A [Export(typeof(IMortgageCalculator))] public class SimpleMortgageCalculator : IMortgageCalculator { public ILogger Logger { get; set; } public float Calculate() { Logger.Log("Calculating Mortgage"); return...; }
Import it… 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...; }
Compose it. Catalogs provide the parts. Catalog
Compose it. Container is the matchmaker. Catalog
Compose it. Container is the matchmaker. Catalog TypeCatalog AssemblyCatalog DirectoryCatalog AggregatingCatalog
Step 1 – MEF Basics
Don’t forget the metadata… Part A [Export(typeof(IMortgageCalculator))] [ExportMetadata(“Calculation”, “Simple”)] [ExportMetadata(“Tax Aware”, null)] public class SimpleMortgageCalculator : IMortgageCalculator { public ILogger Logger { get; set; } public float Calculate() { Logger.Log("Calculating Mortgage"); return...; }
Parts can be lazy… Part A [Import(typeof(ILogger))] public ILogger Logger { get; set; } Part B
Parts can be lazy… Part A Lazy Lazy [Import(typeof(ILogger))] public ILogger Lazy Logger { get; set; }
Step 2 – Metadata and Lazy Evaluation
The slippery slope… Part A [Export(typeof(IMortgageCalculator))] [ExportMetadata(“Calculation”, “Simple”)] [ExportMetadata(“Tax Aware”, null)] [ExportMetadata(“This”, “foo”)] [ExportMetadata(“That”, “bar”)]
The slippery slope… solved Part A [Export(typeof(IMortgageCalculator))] [ExportMetadata(“Calculation”, “Simple”)] [ExportMetadata(“Tax Aware”, null)] [ExportMetadata(“This”, “foo”)] [ExportMetadata(“That”, “bar”)] [Export(typeof(IMortgageCalculator))] [CalcCapabilities( Mode=Complexity.Simple, TaxAware=true, This=“foo”, That=“bar”)]
Step 3 – Strongly-Typed Metadata
The importance of context!
Step 4 – Context Awareness
Lifetime Container Part A Part B Shared Non-Shared
Step 5 – Lifetime
But…… Where are the so-called external dependencies?
.\Extensions CompositionContainer
Step 6 – External Dependencies
The Power of Being Declarative What vs. How
What about System.AddIn?
What about IoC Containers?
What about Hand-Rolled Plugins?
The Problem… Original Software Development Software Maintenance
The Solution… Original Software Development Software Maintenance
Clean Code, Robert C. Martin Working Effectively with Legacy Code, Michael Feathers Refactoring, Martin Fowler Resources