bart j.f. de smet software development engineer microsoft corporation session code: dtl315
DESCRIPTION
Agenda Why extensibility matters Extensibility the naïve way MEF versus System.Addin Core concepts Lots of demos SummaryTRANSCRIPT
Managed Extensibility FrameworkBart J.F. De [email protected]://blogs.bartdesmet.net/bartSoftware Development EngineerMicrosoft CorporationSession Code: DTL315
Agenda
Why extensibility mattersExtensibility the naïve wayMEF versus System.AddinCore conceptsLots of demosSummary
Customer versus developer
Source: http://www.insidefurniture.com Source: http://cristinalaird.files.wordpress.com
Extensibility is hard Looks familiar?
Too hard?
Take a step backinterface 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 );}
Contract
Dynamic load
Name the 10 problems…
Extensibility the naïve wayBart J.F. De SmetSoftware Development EngineerMicrosoft Corporation
demo
Agenda
Why extensibility mattersExtensibility the naïve wayMEF versus System.AddinCore conceptsLots of demosSummary
Managed Extensibility Framework
Partshave 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 nutshellCross-
process or appdomain
Versioning adaptation
The essence of an add-in
Is System.Addin overkill?
System.AddinIsolation boundariesActivation of add-insAcross CLR versions
MEFComposition engine at its coreUtilities for discoverySimple declarative model
Answer: complimentary technologies
Agenda
Why extensibility mattersExtensibility the naïve wayMEF versus System.AddinCore conceptsLots of demosSummary
A world of give and take
Import
Export
Composed
“I need” “I have”
Composition
Host application
Need a IMathBinOp
Extension A
Have a IMathBinOp
MEF composition
engine
[Export] [Import]
Part
Part
Import and Export in practiceinterface 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}
Contract
Import
Export
Host applicationExtension
How to compose?
CompositionContainerWiring “surface” where magic happens
CompositionBatchContains the parts to be wired together
ContainerBatch
PartCompose
Import and Export in practiceclass 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 compositionBart J.F. De SmetSoftware Development EngineerMicrosoft Corporation
demo
Advanced topics in import/export
Named contractsTypically based on TypeCan also be string-based
ImportManyFor use with collectionsE.g. calculator has many operations
Instantiation controlWhich constructor to run?Part creation policies (sharing of instances)
Where do parts come from?
Concept of a catalogType catalog – explicit list of typesAssembly catalog – attributed types in an assemblyDirectory catalog – based on directory searchAggregate catalog – allows combining catalogs
Union operation
ComposablePartCatalog base classQueryable for parts (Parts)Search exports for a given import (GetExports)
Using a DirectoryCatalogclass 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 catalogsBart J.F. De SmetSoftware Development EngineerMicrosoft Corporation
demo
Agenda
Why extensibility mattersExtensibility the naïve wayMEF versus System.AddinCore conceptsLots of demosSummary
Selection of MEF Preview 6 samplesBart J.F. De SmetSoftware Development EngineerMicrosoft Corporation
demo
http://mef.codeplex.com
Agenda
Why extensibility mattersExtensibility the naïve wayMEF versus System.AddinCore conceptsLots of demosSummary
The ABC CCC of MEF
Extensibility
Compose
CatalogContract
question & answer
www.microsoft.com/teched
Sessions On-Demand & Community
http://microsoft.com/technet
Resources for IT Professionals
http://microsoft.com/msdn
Resources for Developers
www.microsoft.com/learning
Microsoft Certification & Training Resources
Resources
Complete an evaluation on CommNet and enter to win!
© 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.