Presentation is loading. Please wait.

Presentation is loading. Please wait.

Present by Andie Saizan, MCP

Similar presentations


Presentation on theme: "Present by Andie Saizan, MCP"— Presentation transcript:

1 Present by Andie Saizan, MCP
Get addicted to MEF and use it in your architecture Managed Extensibility Framework (MEF) Present by Andie Saizan, MCP

2 It’s part of .Net! MEF is a component of .NET Framework aiming to create lightweight, extensible applications. It aims to allow .NET application developers to discover and use extensions with no configuration required. It also aims to allow developers encapsulate code easily and avoid fragile hard dependencies. Furthermore, it aims to allow extensions to be reused across applications. MEF was introduced as a part of .NET 4.0

3 Dependency Injection (DI)? Inversion of Control (IoC)?
IoC is a design pattern with the goal of reducing awareness of concrete implementations from application framework code and giving more control to the domain specific components of your application. Dependency injection is a pattern used to create instances of classes that other classes rely on without knowing at compile time which implementation will be used to provide that functionality. IoC can utilize DI because a mechanism is needed in order to create the components providing the specific functionality.

4 How easy is it to implement?
Reference System.ComponentModel.Composition Use the Export Attribute on classes you want to make injectable Use the Import, ImportMany, or the ImportingContructor in the classes where you want to inject To inject create a Composition Container and a Catalog This is generally done with a bootstrap class that loads during startup. Catalogs can be created from an assembly contained in the project, from a directory that contains assemblies, or a combination of both.

5 Lifetime Management CreationPolicy Enumeration found in System
Lifetime Management CreationPolicy Enumeration found in System.ComponentModel.Composition Member name Description Any Specifies that the CompositionContainer will use the most appropriate CreationPolicy for the part given the current context. This is the default CreationPolicy. By default, CompositionContainer will use Shared, unless the ComposablePart or importer requests NonShared. NonShared Specifies that a new non-shared instance of the associated ComposablePart will be created by the CompositionContainer for every requestor. Shared Specifies that a single shared instance of the associated ComposablePart will be created by the CompositionContainer and shared by all requestors. (Singleton)

6 Export Factory This works with the non-shared partial creation policy and allows the injection of an ExportFactory<T>, which is used within the injected class to create instances of T. This is used when you have the need to create multiple instances of an object, that contain DI objects, shared and/or non-shared.

7 ImportMany Provider Pattern
Create an IProcessor with multiple implementations. Create a ProcessorProvider and use MEF to Import all implementations of IProcessor. In the Provide method you query you Processors that meet the criteria specified to provide a Processor. Very useful in WPF applications when working with Controls and Views. A common pattern in WPF applications is to have a region for some functionality, and then page through multiple controls within that region. For example, a wizard might occupy the main region of the page but actually use four different controls for each of four different steps in the wizard. MEF makes it easy to handle this pattern using ImportMany. I can ask for an IWizard and then retrieve a list of controls that satisfy the interface.

8 The Power Of Metadata Think lazy loading.
MEF allows you to supply strongly-typed metadata for your exports. Metadata can provide initialization information, routing information, and even priority information. You can create and inherit from MetadataAttributes and Export classes with the metadata, allowing you to use Lazy Loading. This allows object not to be initialized until they are used. You can also access the metadata without initializing the object. This can be very beneficial in large scale WPF applications with lots of Controls/Views. All the controls can be imported into a View manager and accesses or navigated to as need by evaluating the metadata.

9 Extensibility, Extensibility, Extensibility!
MEF is unique in that it is designed specifically for extensibility. Many Dependency Injection/Inversion of Control solutions are designed to help you wire up what you know - "I have this instance, map it to this interface." MEF is designed to help you wire up what you don't know. It is extensible by nature and contains features such as stable composition that ensure your application can safely extend in real-time. You can build an application using MEF. While the application is running you can build a plugin, drop it into the target directory, and the application is able to pull in the new plugin in real-time without restarting.

10 Modularity Modularity is most beneficial in WPF and Windows Store Apps. MEF allows for modularity by allowing you to separate functionality into different modules and load them dynamically as needed. Imagine a large application that has ten menu items, but users typically may only go into one. Instead of loading everything , MEF allows you to split your applications into modules that load on demand. Only when the user needs a piece of functionality, will the application load the extension and make it available. This allows them to get up and running faster and conserves resources by only supplying what is needed.

11 Unit Testing With MEF Because of dependency injection, unit testing applications can be done with ease and you have options. You can create and compose a container with fake or mocked classes You can test individual parts of you application and mock the data for the imports without composition

12 Why I love MEF and why you should to!
It’s part of the .Net Library. No add-ins, no plugins, it’s just there already. It handles both Dependency Injection and IoC, with minimal code. Lifetime management with Partial Creation Policy Handle application configuration data with ease Can use Factories for dependency resolution. ImportMany, which is one of my favorites because it works to beautifully with the provider pattern. It offers the ability for strongly typed metadata and lazy loading. It make extending applications done with ease. Modularity! MEF makes applications light weight, only loading what is needed. MEF is configurable and it plays well with others such as Unity and Prism. It’s easily Unit Testable an Mockable

13 Want me to show you the code?


Download ppt "Present by Andie Saizan, MCP"

Similar presentations


Ads by Google