Bart J.F. De Smet Software Development Engineer Microsoft Corporation Session Code: DTL315
Agenda Why extensibility matters Extensibility the naïve way MEF versus System.Addin Core concepts Lots of demos Summary
Extensibility in pictures Source:
Customer versus developer Source: Source:
Extensibility is hard Looks familiar? Too hard?
Take a step back interface IMathBinOp { string Name { get; } int Calculate(int a, int b); } IMathBinOp GetOperation(string path, string type) { Assembly asm = Assembly.LoadFrom(path); return (IMathBinOp)Activator.CreateInstance( asm.Name, type ); } ContractContract Dynamic load Name the 10 problems…
Extensibility the naïve way Bart J.F. De Smet Software Development Engineer Microsoft Corporation
Agenda Why extensibility matters Extensibility the naïve way MEF versus System.Addin Core concepts Lots of demos Summary
Managed Extensibility Framework Parts have a contract are found in a catalog compose in a container Square with 4 pins I have these blocks Let’s build something
System.Addin in a nutshell Cross- process or appdomain Versioning adaptation The essence of an add-in
Is System.Addin overkill? System.Addin Isolation boundaries Activation of add-ins Across CLR versions MEF Composition engine at its core Utilities for discovery Simple declarative model Answer: complimentary technologies
Agenda Why extensibility matters Extensibility the naïve way MEF versus System.Addin Core concepts Lots of demos Summary
A world of give and take ImportExport Composed “I need” “I have”
Composition Host application Need a IMathBinOp Extension A Have a IMathBinOp MEF composition engine [Export][Export] [Import][Import] PartPart PartPart
Import and Export in practice interface IMathBinOp { string Name { get; } int Calculate(int a, int b); } class Calculator { [Import] public IMathBinOp Operator { get; set; } } [Export(typeof(IMathBinOp))] class Add : IMathBinOp { // Implementation } ContractContract ImportImport ExportExport Host application Extension
How to compose? CompositionContainer Wiring “surface” where magic happens CompositionBatch Contains the parts to be wired together ContainerContainer BatchBatch PartPart Compose
Import and Export in practice class App { static void Main() { var calc = new Calculator(); using (var container = new CompositionContainer()) { var batch = new CompositionBatch(); batch.AddPart(calc); batch.AddPart(new Add()); container.Compose(batch); } var res = calc.Operator.Calculate(1,2); } } Manual composition
Simple composition Bart J.F. De Smet Software Development Engineer Microsoft Corporation
Advanced topics in import/export Named contracts Typically based on Type Can also be string-based ImportMany For use with collections E.g. calculator has many operations Instantiation control Which constructor to run? Part creation policies (sharing of instances)
Where do parts come from? Concept of a catalog Type catalog – explicit list of types Assembly catalog – attributed types in an assembly Directory catalog – based on directory search Aggregate catalog – allows combining catalogs Union operation ComposablePartCatalog base class Queryable for parts ( Parts ) Search exports for a given import ( GetExports )
Using a DirectoryCatalog class App { static void Main() { var calc = new Calculator(); var catalog = new DirectoryCatalog(EXTPATH); using (var container = new CompositionContainer(catalog)) { var batch = new CompositionBatch(); batch.AddPart(calc); container.Compose(batch); } var res = calc.Operator.Calculate(1,2); } } Search path
Working with catalogs Bart J.F. De Smet Software Development Engineer Microsoft Corporation
Agenda Why extensibility matters Extensibility the naïve way MEF versus System.Addin Core concepts Lots of demos Summary
Selection of MEF Preview 6 samples Bart J.F. De Smet Software Development Engineer Microsoft Corporation
Agenda Why extensibility matters Extensibility the naïve way MEF versus System.Addin Core concepts Lots of demos Summary
The ABC CCC of MEF Extensibility ComposeCatalogContract
Sessions On-Demand & Community Resources for IT Professionals Resources for Developers Microsoft Certification & Training Resources Resources
Related Content Breakout Sessions (session codes and titles) Interactive Theater Sessions (session codes and titles) Hands-on Labs (session codes and titles)
Track Resources Resource 1 Resource 2 Resource 3 Resource 4
Complete an evaluation on CommNet and enter to win! Required Slide
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION. Required Slide