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

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

Embed Size (px)

DESCRIPTION

by Volodymyr Voytyshyn

Citation preview

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

Volodymyr Voytyshyn03/17/2012

ManagedExtensibility Framework

Effective Development of Scalable Applications with .NET

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

Agenda

What is MEF?

Key concepts and features

How to implement …?

What’s new in MEF 2.0?

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

WHAT IS MEF?

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

The Problem

No unified approach

for extension application in runtime

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

The Solution

MEF – Managed Extensibility Framework

Simply

Reliably

Unified

Page 6: Managed Extensibility Framework or Effective Development of Scalable Applications with .NET 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 or Effective Development of Scalable Applications with .NET Framework

MEF focuses on…

Extending of ready-made applications by plugins

Plugin 1 Plugin 2

Application

?

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

How about reflection?

System.Reflection.*Assembly.Load()

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

As a result…

The next unique solution

Not reusable components

Extending by 3rd parties plugins

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

KEY CONCEPTS AND FEATURES

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

Composable Part

Part

Export

Import

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

More Composable Parts

ImportImport

ExportExport

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

The Main Idea - Composition

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

Composition Container

Composition Container

Creates parts

Dispose parts

Compose parts

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

The Part

Typed Contract

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

The Part

Named Contract

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

The Part

Extension Point

Extension Point

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

Composition of Parts

Compositionof parts

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

Don’t forget…

Add reference

System.ComponentModel.Composition.dll

Using namespaces

System.ComponentModel.Composition.*

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

More About Import

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

Export Metadata

Key

Value

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

Getting Exports

Contract

Returns Lazy

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

Creation Policy

Default value

Shared

NonShared

Any

Shared == Singleton

Any & Any == Shared

Page 24: Managed Extensibility Framework or Effective Development of Scalable Applications with .NET 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 or Effective Development of Scalable Applications with .NET 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 or Effective Development of Scalable Applications with .NET Framework

Catalog

Catalog

CompositionContainer

Discover parts

Compose parts

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

Catalog Types

Assembly Catalog

Directory Catalog

Type Catalog

Aggregate Catalog

Deployment Catalog(only for Silverlight)

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

Recomposition Support

Assembly Catalog

Directory Catalog Refresh()

Type Catalog

Aggregate Catalog auto

Deployment Catalog auto

Page 29: Managed Extensibility Framework or Effective Development of Scalable Applications with .NET Framework
Page 30: Managed Extensibility Framework or Effective Development of Scalable Applications with .NET Framework

HOW TO IMPLEMENT… ?

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

MEF-based Dependency Injection

CompositionContainer

Service Locator

TPart Resolve<TPart>()

void Release<TPart>(TPart part) CommonService Locator

MEF Adapter

Page 32: Managed Extensibility Framework or Effective Development of Scalable Applications with .NET 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 or Effective Development of Scalable Applications with .NET Framework

Plugin-based Desktop Application

Most Useful Tool

Directory Catalog

Composition Container

File System Watcher

Plugins Folder

Trackchanges

Refresh()

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

Plugin-based Desktop Application

Allow parts recomposition

Assemblies shadow copy

! Plugin assemblycan not be unloaded in runtime

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

Plugin-based ASP.NET MVC Application

ASP.NET MVC Application

Models Views Plugins

Models Views

Controlle

rs

Controlle

rs

Page 36: Managed Extensibility Framework or Effective Development of Scalable Applications with .NET 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 or Effective Development of Scalable Applications with .NET Framework

WHAT’S NEW IN MEF 2.0?

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

MEF 2.0 and .NET 4.5

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

Open Generic Parts

Any type

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

Convention-based Part Registration

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

Composition Scoping Enhancements

Global Scope

Request Scope

Composition Container

Logger

Customer Controller

Customer RepositoryFor each request

Once

Page 42: Managed Extensibility Framework or Effective Development of Scalable Applications with .NET 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 or Effective Development of Scalable Applications with .NET Framework

ASP.NET MVC: processing of parts

Naming convention

Contract identification

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

Composition Provider for ASP.NET MVC Composition scopes

– Request scope

– Application scope

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

MEF Offers…

Easy way for:

Composition independent components

Extending without recompiling

Delivering 3rd party plugins

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

Ask your question…

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

Thank You!

Copyright © 2011 SoftServe, Inc.

Contacts

Volodymyr Voytyshyn5 Knyahyni Olhy Str., Rivne 33000, Ukraine

Tel: +38(066) 41 83 509

E-mail: [email protected]@gmail.com

Skype: voytyshin