Managed
            Extensibility Framework
Effective Development of Scalable Applications with .NET
                                                 Volodymyr Voytyshyn
                                                         03/17/2012
Agenda

 What is MEF?

 Key concepts and features

 How to implement …?

 What’s new in MEF 2.0?
WHAT IS MEF?
The Problem

          No unified approach
   for extension application in runtime
The Solution

 MEF – Managed Extensibility Framework


  Simply

  Reliably

  Unified
MEF Project
 Official Site      http://mef.codeplex.com


 Developer          Microsoft


 Started            26.08.2008


 License            MS Public License
                     (open source)

 Last stable release MEF 1 / 12.04.2010
                     (included into .NET 4.0)
MEF focuses on…

 Extending of ready-made applications by plugins




                  Application




      Plugin 1       Plugin 2
How about reflection?

       System.Reflection.*
         Assembly.Load()
                …
As a result…


 The next unique solution

 Not reusable components

 Extending by 3rd parties
  plugins
KEY CONCEPTS AND FEATURES
Composable Part



                         Export


                  Part
      Import
More Composable Parts



  Import
   Import

                        Export
                         Export
The Main Idea - Composition
Composition Container

                        Composition Container
 Compose parts




                     Creates parts

                     Dispose parts
The Part

           Typed Contract
The Part

           Named Contract
The Part

           Extension
             Point




           Extension
             Point
Composition of Parts




  Composition
    of parts
Don’t forget…

 Add reference

System.ComponentModel.Composition.dll


 Using namespaces

 System.ComponentModel.Composition.*
More About Import
Export Metadata


              Key




                    Value
Getting Exports

                        Contract




         Returns Lazy
Creation Policy




          Shared
                                   Default
          NonShared                value
          Any




      Any & Any == Shared
                     Shared == Singleton
References to Parts

 Container keeps reference to part if:

  – Part is marked as Shared

  – Part implements IDisposable

  – One or more imports allow Recomposition
Disposing Composition Container

 Dispose parts which implement IDisposable

 Reference to parts will be cleaned up

 Lazy exports won’t work
Catalog
            Composition
             Container
                          Compose
                            parts
 Discover
  parts
             Catalog
Catalog Types

        Assembly Catalog

        Directory Catalog

        Type Catalog

        Aggregate Catalog

        Deployment Catalog
        (only for Silverlight)
Recomposition Support

 Assembly Catalog

 Directory Catalog     Refresh()

 Type Catalog

 Aggregate Catalog     auto


 Deployment Catalog    auto
HOW TO IMPLEMENT… ?
MEF-based Dependency Injection

       Service Locator

  TPart Resolve<TPart>()



  void Release<TPart>(TPart part)      Common
                                    Service Locator




           Composition
            Container               MEF Adapter
MEF vs IoC
+ Ease of programming

+ Resolves dependencies between components

+ Automatic component discovery

+ Can compose types, fields, props and methods


- Slower than IoC containers

- Poor component lifetime management

- No component separation (separate appdomain, process)
Plugin-based Desktop Application

      Most Useful Tool

             Composition
              Container             Plugins Folder


              Directory
               Catalog

 Refresh()

                               Track
        File System Watcher
                              changes
Plugin-based Desktop Application

 Allow parts recomposition

 Assemblies shadow copy


! Plugin assembly
  can not be unloaded in runtime
Plugin-based ASP.NET MVC Application


           ASP.NET MVC Application


 Models   Views                  Plugins



                        Models    Views
Plugin-based ASP.NET MVC Application

Integrate MEF into ControllerFactory

MEF-based DependencyResolver


? Compile razor view to separate assembly
WHAT’S NEW IN MEF 2.0?
MEF 2.0 and .NET 4.5
Open Generic Parts

                     Any type
Convention-based Part Registration
Composition Scoping Enhancements

   Once                            Global Scope

                                      Logger

  Composition Container


                               Request Scope

                          Customer Controller

 For each request             Customer Repository
Composition Provider for ASP.NET MVC

 Registering in composition container:

   – Controllers
   – Action filters
   – Model binders


 Constructor injection for controllers
ASP.NET MVC: processing of parts

               Naming
             convention




              Contract
            identification
Composition Provider for ASP.NET MVC

 Composition scopes

  – Request scope

  – Application scope
MEF Offers…
 Easy way for:

   Composition independent components

   Extending without recompiling

   Delivering 3rd party plugins
Ask your question…
Useful Sources
 MEF Project (official site)

 Managed Extensibility Framework Overview (msdn)

 Building Composable Apps in .NET 4 with MEF (msdn)

 BCL Team Blog (msdn)

 Book of MEF (habrahabr)
Contacts

                                   Volodymyr Voytyshyn
                                   5 Knyahyni Olhy Str., Rivne 33000, Ukraine


                                   Tel:       +38(066) 41 83 509

                                   E-mail:    vvoit@softserveinc.com
                                              voytyshyn@gmail.com

                                   Skype:     voytyshin




                                   Thank You!


Copyright © 2011 SoftServe, Inc.

Managed Extensibility Framework or Effective Development of Scalable Applications with .NET Framework