managed extensibility framework
TRANSCRIPT
Volodymyr Voytyshyn
03/17/2012
Managed
Extensibility Framework
Effective Development of Scalable Applications with .NET
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 extending applications 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
Plugin 1 Plugin 2
Application
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
Part
Export
Import
More Composable Parts
ImportImport
ExportExport
The Main Idea - Composition
Composition Container
Composition Container
� Create parts
� Dispose parts
� Compose parts
Part Sample
Typed Contract
Part Sample
Named Contract
Part Sample
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 from Container
Contract
Returns Lazy
Creation Policy
Default
value
� Shared
� NonShared
� Any
� Shared == Singleton
� Any & Any == Shared
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
Catalog
Composition
Container
Discover
parts
Compose
parts
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
Composition
Container
Service Locator
TPart Resolve <TPart>()
void Release <TPart>(TPart part) Common
Service Locator
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
Directory
Catalog
Composition
Container
File System Watcher
Plugins Folder
Track
changes
Refresh()
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
Global Scope
Request Scope
Composition Container
Logger
Customer Controller
Customer RepositoryFor each request
Once
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)
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]
Skype: voytyshin