managed extensibility framework

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

Upload: volodymyr-voytyshyn

Post on 06-May-2015

1.121 views

Category:

Technology


9 download

TRANSCRIPT

Page 1: Managed Extensibility Framework

Volodymyr Voytyshyn

03/17/2012

Managed

Extensibility Framework

Effective Development of Scalable Applications with .NET

Page 2: Managed Extensibility Framework

Agenda

� What is MEF?

� Key concepts and features

� How to implement …?

� What’s new in MEF 2.0?

Page 3: Managed Extensibility Framework

WHAT IS MEF?

Page 4: Managed Extensibility Framework

The Problem

No unified approach

for extending applications in runtime

Page 5: Managed Extensibility Framework

The Solution

�MEF – Managed Extensibility Framework

� Simply

� Reliably

� Unified

Page 6: Managed Extensibility Framework

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)

Page 7: Managed Extensibility Framework

MEF focuses on…

� extending of ready-made applications

Plugin 1 Plugin 2

Application

Page 8: Managed Extensibility Framework

How about reflection?

System.Reflection .*

Assembly.Load ()

Page 9: Managed Extensibility Framework

As a result…

� The next unique solution

� Not reusable components

� Extending by 3rd parties plugins

Page 10: Managed Extensibility Framework

KEY CONCEPTS AND FEATURES

Page 11: Managed Extensibility Framework

Composable Part

Part

Export

Import

Page 12: Managed Extensibility Framework

More Composable Parts

ImportImport

ExportExport

Page 13: Managed Extensibility Framework

The Main Idea - Composition

Page 14: Managed Extensibility Framework

Composition Container

Composition Container

� Create parts

� Dispose parts

� Compose parts

Page 15: Managed Extensibility Framework

Part Sample

Typed Contract

Page 16: Managed Extensibility Framework

Part Sample

Named Contract

Page 17: Managed Extensibility Framework

Part Sample

Extension

Point

Extension

Point

Page 18: Managed Extensibility Framework

Composition of Parts

Composition

of parts

Page 19: Managed Extensibility Framework

Don’t forget…

� Add reference

System.ComponentModel.Composition.dll

� Using namespaces

System.ComponentModel.Composition .*

Page 20: Managed Extensibility Framework

More About Import

Page 21: Managed Extensibility Framework

Export Metadata

Key

Value

Page 22: Managed Extensibility Framework

Getting Exports from Container

Contract

Returns Lazy

Page 23: Managed Extensibility Framework

Creation Policy

Default

value

� Shared

� NonShared

� Any

� Shared == Singleton

� Any & Any == Shared

Page 24: Managed Extensibility Framework

References to Parts

� Container keeps reference to part if:

– Part is marked as Shared

– Part implements IDisposable

– One or more imports allow Recomposition

Page 25: Managed Extensibility Framework

Disposing Composition Container

� Dispose parts which implement IDisposable

� Reference to parts will be cleaned up

� Lazy exports won’t work

Page 26: Managed Extensibility Framework

Catalog

Catalog

Composition

Container

Discover

parts

Compose

parts

Page 27: Managed Extensibility Framework

Catalog Types

Assembly Catalog

Directory Catalog

Type Catalog

Aggregate Catalog

Deployment Catalog

(only for Silverlight)

Page 28: Managed Extensibility Framework

Recomposition Support

� Assembly Catalog

� Directory Catalog Refresh()

� Type Catalog

� Aggregate Catalog auto

� Deployment Catalog auto

Page 29: Managed Extensibility Framework
Page 30: Managed Extensibility Framework

HOW TO IMPLEMENT… ?

Page 31: Managed Extensibility Framework

MEF-based Dependency Injection

Composition

Container

Service Locator

TPart Resolve <TPart>()

void Release <TPart>(TPart part) Common

Service Locator

MEF Adapter

Page 32: Managed Extensibility Framework

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)

Page 33: Managed Extensibility Framework

Plugin-based Desktop Application

Most Useful Tool

Directory

Catalog

Composition

Container

File System Watcher

Plugins Folder

Track

changes

Refresh()

Page 34: Managed Extensibility Framework

Plugin-based Desktop Application

�Allow parts recomposition

�Assemblies shadow copy

! Plugin assembly

can not be unloaded in runtime

Page 35: Managed Extensibility Framework

Plugin-based ASP.NET MVC Application

ASP.NET MVC Application

Models Views Plugins

Models Views

Page 36: Managed Extensibility Framework

Plugin-based ASP.NET MVC Application

�Integrate MEF into ControllerFactory

�MEF-based DependencyResolver

? Compile razor view to separate assembly

Page 37: Managed Extensibility Framework

WHAT’S NEW IN MEF 2.0?

Page 38: Managed Extensibility Framework

MEF 2.0 and .NET 4.5

Page 39: Managed Extensibility Framework

Open Generic Parts

Any type

Page 40: Managed Extensibility Framework

Convention-based Part Registration

Page 41: Managed Extensibility Framework

Composition Scoping Enhancements

Global Scope

Request Scope

Composition Container

Logger

Customer Controller

Customer RepositoryFor each request

Once

Page 42: Managed Extensibility Framework

Composition Provider for ASP.NET MVC

� Registering in composition container:

– Controllers

– Action filters

– Model binders

� Constructor injection for controllers

Page 43: Managed Extensibility Framework

ASP.NET MVC: processing of parts

Naming

convention

Contract

identification

Page 44: Managed Extensibility Framework

Composition Provider for ASP.NET MVC

� Composition scopes

– Request scope

– Application scope

Page 45: Managed Extensibility Framework

MEF Offers…

� Easy way for:

� Composition independent components

� Extending without recompiling

� Delivering 3rd party plugins

Page 46: Managed Extensibility Framework

Ask your question…

Page 47: Managed Extensibility Framework

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)

Page 48: Managed Extensibility Framework

Thank You!

Copyright © 2011 SoftServe, Inc.

Contacts

Volodymyr Voytyshyn

5 Knyahyni Olhy Str., Rivne 33000, Ukraine

Tel: +38(066) 41 83 509

E-mail: [email protected]

[email protected]

Skype: voytyshin