8. Reality We need….. We need….. We need….. We need….. K We need….. F A E We need….. J I D RIA 1.0 C B H G We need….. We need….. We need…. We need….
9. Managed Extensibility Framework The Managed Extensibility Framework (MEF) is a new library in Silverlight 4 for building applications that can be incrementally extended. For customers For you and your team Always there, always ready
13. Export it. [Export(typeof(UserControl))] public class Widget1 : UserControl { public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } } Widget1 UserControl Export
14. Import it. [Export(typeof(UserControl))] public class Widget1 : UserControl { [Import] public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } } Widget1 String Import
15. Import it. [Export(typeof(UserControl))] public class Widget1 : UserControl { [Import(“HelloMEF.Message”)] public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } } Widget1 “HelloMEF.Message” Import
16. Import it. [Export(typeof(UserControl))] public class MainPage: UserControl { [ImportMany(typeof(UserControl))] public IEnumerable<UserControl> { get;set; } } Main Page UserControl ImportMany
17. Compose it. PartIntializer: “Compose yourself” public MainPage() { InitializeComponent(); PartInitializer.SatisfyImports(this); } MainPage Compose
20. Export it - Metadata [ExportMetadata(“Location”,Location.Top)] [Export(typeof(UserControl))] public class Widget1 : UserControl { public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } } Widget1 Put me in the top UserControl Export
21. Import it - Metadata [Export(typeof(UserControl))] public class MainPage: UserControl { [ImportMany(typeof(UserControl))] public IEnumerable<Lazy<UserControl, IWidgetMetadata> { get;set; } } Main Page UserControl ImportMany
23. Export it - Metadata [ExportMetadata(“Location”,Location.Top)] [Export(typeof(UserControl))] public class Widget1 : UserControl { public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } } Widget1 Put me in the top UserControl Export
24. Customize it – Custom exports [Widget(Location=Location.Top)] public class Widget1 : UserControl { public string Message { get{return(string) Button.Content;} set{Button.Content=value;} } } Widget1 Put me in the top UserControl Export
27. MEF and Prism Ariel Ben-Horesh arielb@sela.co.il blogs.microsoft.co.il/blogs/arielbh Developer Sela Group partner
28. Introducing MEF-Prism integration Our Goal : Enable an existing Prism application to be further extended through MEF Enabled MEF modules to be added dynamically on the fly. Enable MEF discovered components to be registered in IoC container (Such as Unity).
30. Introducing MEF-Prism integration Prism application can load unknownplugins, by using MEF as an abstraction layer. 1. Plugins are located and added to a MEF catalog. Server
31. Introducing MEF-Prism integration Prism application can load unknownplugins, by using MEF as an abstraction layer. 2. Plugins register dependencies with current IoC container. Server
32. Introducing MEF-Prism integration Prism application can load unknownplugins, by using MEF as an abstraction layer. 3. Plugins are activated. Dependencies are injected. Server
33. Where can I get it? MEF ships in Silverlight 4.0 Dynamic XAP support in SL 4.0 Toolkit Silverlight 3.0 support on Codeplex MEF source ships under MSPL Learn more: Silverlight.net MEF.Codeplex.com MEFContrib.Codeplex.com – Prism/MEF blogs.msdn.com/gblock
MESSAGING:New Library = just an assembly, a normal .dll, used by any .NET languageDynamically Composed = composition takes place at runtime and composition behavior differs depending on how it is configured.In making applications dynamically composed, MEF allows you to build systems that adhere to this “Open/Closed principle, in other words it makes it easy to design systems which can adapt to change.But MEF goes further than simply allow you to extend your own system, it provides a means to allow third parties to extend the application after it has been deployed.
There are really three concepts you need to understand with MEFExport It – Components (we call them Parts) provide capabilities to other parts called Exports. To Export you decorate your class with an Export attribute essentially saying, “Here I am providing this”Import It – To use the capabilities of another part, you Import it. To import your decorate a property on your part with an Import attribute which means “I need this”Compose It – The last step is putting the pieces together. The container (CompositionContainer) is responsible for that job. We’ll see how that works soon.
MESSAGING:Now in the case of Exports, exports are contracts a part offers to other parts. In the case of our DiscountRule, we are exporting ourself as an IRule. This is done very easily by decorating our class with an Export class and specifying that we would like to export ourselves specifically as an IRule (it’s important to note, that if you left out “typeof(IRule)”, the class would be exported as a DiscountRule instead (so other parts importing IRule would not find us)).Once again, very simple. Decorate with an Export attribute and MEF has all the information it needs for this specific part.
MESSAGING:In the case of our DiscountRule, we simply decorate our ILogger property with an Import attribute that specifies what type we need to import (this is because we could very well be importing a different type of contract than is satisfied directly (see the slide on Lazy for more info)). This is how simple it is. Decorate with an Import attribute and MEF has all the information it needs for this specific part.NOTES:This could also be done on a constructor that accepts an ILogger and uses the ImportingConstructor attribute.
MESSAGING:In the case of our DiscountRule, we simply decorate our ILogger property with an Import attribute that specifies what type we need to import (this is because we could very well be importing a different type of contract than is satisfied directly (see the slide on Lazy for more info)). This is how simple it is. Decorate with an Import attribute and MEF has all the information it needs for this specific part.NOTES:This could also be done on a constructor that accepts an ILogger and uses the ImportingConstructor attribute.
MESSAGING:Now this is good for a single import, but what about when I need to import many of a specific export? For example our RulesEngine imports a collection of rules. In this case we use an ImportManyAttribute on a collection.Note if we do ImportMany on a constuctor we must specify the [ImportMany] attribute explicitly.
MESSAGING:In the desktop there’s a bunch of work you need to do to get the composition going including creating a container and the catalogs. There’s also challenges particularly in XAML scenarios where the XAML runtime creates things like UserControls which don’t’ have access to the container. In Silverlight, we wanted to make it as simple as possible to get-going using MEF in your applications. There’s no need to create container or a catalog. We will automatically create a container for you and add to all the assemblies in the current XAP. All you need to do is call SatisfyImports on PartInitializer and we’ll take care of the rest.
MESSAGING:Now in the case of Exports, exports are contracts a part offers to other parts. In the case of our DiscountRule, we are exporting ourself as an IRule. This is done very easily by decorating our class with an Export class and specifying that we would like to export ourselves specifically as an IRule (it’s important to note, that if you left out “typeof(IRule)”, the class would be exported as a DiscountRule instead (so other parts importing IRule would not find us)).Once again, very simple. Decorate with an Export attribute and MEF has all the information it needs for this specific part.
MESSAGING:Now this is good for a single import, but what about when I need to import many of a specific export? For example our RulesEngine imports a collection of rules. In this case we use an ImportManyAttribute on a collection.Note if we do ImportMany on a constuctor we must specify the [ImportMany] attribute explicitly.
MESSAGING:Now in the case of Exports, exports are contracts a part offers to other parts. In the case of our DiscountRule, we are exporting ourself as an IRule. This is done very easily by decorating our class with an Export class and specifying that we would like to export ourselves specifically as an IRule (it’s important to note, that if you left out “typeof(IRule)”, the class would be exported as a DiscountRule instead (so other parts importing IRule would not find us)).Once again, very simple. Decorate with an Export attribute and MEF has all the information it needs for this specific part.
MESSAGING:Now in the case of Exports, exports are contracts a part offers to other parts. In the case of our DiscountRule, we are exporting ourself as an IRule. This is done very easily by decorating our class with an Export class and specifying that we would like to export ourselves specifically as an IRule (it’s important to note, that if you left out “typeof(IRule)”, the class would be exported as a DiscountRule instead (so other parts importing IRule would not find us)).Once again, very simple. Decorate with an Export attribute and MEF has all the information it needs for this specific part.