MEF & Silverlight 4: Extensible ApplicationsApplicazioni estensibili impiegando MEF e Silverlight 4
» Davide Zordan – MVP Silverlight, Silverlight Insider
www.xedotnet.org 2
AgendaAgenda
30/04/2010
Cos’è MEF? A cosa serve?
Pattern e framework applicativi: Prism, MEF & beyond
Introduzione a MEF: Export, Import, Compose
Custom Attributes e Metadata
Download dinamico mediante il DeploymentCatalog
MEF e pattern MVVM: un caso pratico
www.xedotnet.org 330/04/2010
MEF (Managed Extensibility Framework) è una nuova libreria presente in .NET Framework 4 e Silverlight 4 per
semplificare la progettazione di sistemi componibili, estensibili e componibili anche dopo il deploy.
Cos’è MEF
www.xedotnet.org 417/01/2008
Perchè creare delle Applicazioni estensibili: • Per risolvere un problema comune:
creare applicazioni da componenti riutilizzabili che possono essere scoperte, riutilizzare e composte in modo dinamico• es. sistema a plug-in di Visual Studio;• semplificare la manutenzione del
codice;• semplificare l’aggiunta di nuove
funzionalità;• rendere il codice più elegante.
Perché MEF
www.xedotnet.org 530/04/2010
Concetti fondamentali:
• Export, Import, Compose, Metadata• Export -> è un servizio che fornisce un
componente (Part);• Import -> possono importare singoli o più servizi
(esempio un logger o un'estensione di editor);• Compose -> le parti sono composte da MEF;• Metadata -> specificano informazioni aggiuntive
relative alle «Part» esposte.
MEF: concetti fondamentali
www.xedotnet.org 630/04/2010
[Export]public partial class ShellView : UserControl{
public ShellView () {
InitializeComponent(); }
}
Esportazione di una classe:
www.xedotnet.org 730/04/2010
public class Loggerpart{ [Export]
public ILog Logger {
get { return LogManager.GetLogger("Logger"); } }
}
Esportazione tramite proprietà:
www.xedotnet.org 830/04/2010
[Export]public class ViewFactory{ [Import] public ShellView shellView { get; set; }}
Importazione tramite proprieta' e campi
www.xedotnet.org 930/04/2010
[Export]public class ViewFactory{ [ImportingConstructor] public ViewFactory(ShellView shellView) { }}
Importazione tramite costruttori
www.xedotnet.org 1030/04/2010
Composizione mediante il CompositionInitializer:
CompositionInitializer.SatisfyImports(this);
Composizione
www.xedotnet.org 1130/04/2010
[PartCreationPolicy(CreationPolicy.NonShared)][Export(typeof(IShellView))]public partial class ShellView : UserControl, IShellView{
public ShellView() { }
}
PartCreationPolicy
www.xedotnet.org 1230/04/2010
[Export]public class ViewFactory{ [ImportMany] IEnumerable<IView> Views { get; set; }}[Export(typeof(IView))]public partial class ShellView : UserControl, IView{}public interface IView{}
Esportazioni multiple
www.xedotnet.org 1330/04/2010
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public class ExportMainPageVMAttribute : ExportAttribute { public ExportMainPageVMAttribute(): base(typeof(ViewModelBase)) { } }
Attributi personalizzati di esportazione
www.xedotnet.org 1430/04/2010
public partial class MainPageView : UserControl { public MainPageView() { InitializeComponent();
CompositionInitializer.SatisfyImports(this); this.DataContext = mainPageViewModel; }
[ImportMainPageVMAttribute] public object mainPageViewModel { get; set; } }
Utilizzo
www.xedotnet.org 1530/04/2010
public class SimpleMEFProvider { [Export("SimpleText")] [ExportMetadata("Index",0)] public string aSimpleTextProvider { get { return "This is a simple Text MEF Imported"; } }
[Export("SimpleText")] [ExportMetadata("Index",1)] public string anotherSimpleTextProvider { get { return "This is another simple Text MEF Imported"; } } }
Esportazioni multiple e Metadata
www.xedotnet.org 1630/04/2010
public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); CompositionInitializer.SatisfyImports(this); }
//Multiple Exports detected: use ImportMany [ImportMany("SimpleText")] public IEnumerable<Lazy<string,IMetadataContent>> textProperty { get; set; }
//Used to retrieve the value of the Metadata Exported by MEF public interface IMetadataContent { int Index { get; } } }
Import Metadata
www.xedotnet.org 1730/04/2010
Demo 1: Simple Composition
OK OK, SHOW ME THE SAMPLES
www.xedotnet.org 1830/04/2010
Demo 1: Export, Import, Compose!
DEMO 1: EXPORT, IMPORT, COMPOSE!
www.xedotnet.org 1930/04/2010
Demo 2: Export multipli
DEMO 2: EXPORT MULTIPLI
www.xedotnet.org 2030/04/2010
Demo 3: Export multipli
DEMO 3: EXPORT METADATA
www.xedotnet.org 2130/04/2010
Demo 4: Simple MEF-MVVM
DEMO 4: SIMPLE MEF-MVVM
www.xedotnet.org 2230/04/2010
Demo 5: Simple MEF-MVVM
DEMO 5: ATTRIBUTI CUSTOM
www.xedotnet.org 2330/04/2010
Demo 6: Export Factory
DEMO 6: EXPORT FACTORY
www.xedotnet.org 2430/04/2010
Demo 7: MEF & (PRISM) Event Aggregator
DEMO 7: MEF & (PRISM) EVENT AGGREGATOR
www.xedotnet.org 2530/04/2010
Demo 8: MEF & DeploymentCatalog
DEMO 8: MEF & DEPLOYMENT
CATALOG
www.xedotnet.org 2630/04/2010
Cos’è MEF
Perché MEF
Concetti fondamentali: Export, Import, Compose, Contracts
Composizioni semplici e multiple, Metadata
Attributi Custom, Export Factory
MEF e MVVM
Deploy dinamico mediante il DeploymentCatalog
Riepilogo
www.xedotnet.org 27
blog:
email:
web:
twitter:
Link
30/04/2010
How to reach me
http://www.davidezordan.net/blog
http://silverlightexperts.net
http://twitter.com/davidezordan