+ All Categories
Home > Technology > Extending the Enterprise with MEF

Extending the Enterprise with MEF

Date post: 08-Jul-2015
Category:
Upload: brian-ritchie
View: 913 times
Download: 4 times
Share this document with a friend
Description:
Building extensibility into your enterprise apps with the Managed Extensibility Framework
Popular Tags:
24
Extending the Enterprise Extending the Enterprise with MEF with MEF Brian Ritchie Chief Architect PaySpan, Inc. Twitter: @brian_ritchie Email: [email protected] Blog: http://weblog.asp.net/britchie Web: http://www.dotnetpowered.com Building extensibility into your enterprise apps with the Managed Extensibility Framework Jason Gerard Director of Product Development PaySpan, Inc. Twitter: @thejasongerard Email: [email protected] Blog: http://jasongerard.wordpress.com
Transcript
Page 1: Extending the Enterprise with MEF

Extending the Enterprise Extending the Enterprise with MEFwith MEF

Brian RitchieChief ArchitectPaySpan, Inc.

Twitter: @brian_ritchieEmail: [email protected]: http://weblog.asp.net/britchieWeb: http://www.dotnetpowered.com

Building extensibility into your enterprise apps with the Managed Extensibility Framework

Jason GerardDirector of Product DevelopmentPaySpan, Inc.

Twitter: @thejasongerardEmail: [email protected]: http://jasongerard.wordpress.com

Page 2: Extending the Enterprise with MEF

Who are we?Who are we?

» Chief Architect

» Nearly 20 years of development experience

» Developing on .NET since 1.0 Beta 1. Contributor to Mono and other open source projects

Brian Ritchie» Director of Development

» Nearly 15 years of development experience

» Only fault is that he is only one man

Jason Gerard

Page 3: Extending the Enterprise with MEF

Managed Extensibility FrameworkManaged Extensibility Framework

Agenda»What is MEF?»When to use MEF»Benefits of using MEF»Key Concepts»What’s new in .NET 4.5?»Demo!

Page 4: Extending the Enterprise with MEF

What is MEF?What is MEF?

The Managed Extensibility Framework or MEF is a library for creating lightweight, extensible applications.

»It allows application developers to discover and use extensions with no configuration required. It lets extension developers easily encapsulate code and avoid fragile hard dependencies. MEF not only allows extensions to be reused within applications, but across applications as well.

Page 5: Extending the Enterprise with MEF

When to use MEF?When to use MEF?

And when not to...And when not to...

Page 6: Extending the Enterprise with MEF

When to use MEF?When to use MEF?

» You are planning to star in a zombie movie» You weren’t born in Britain but want British

teeth.NO!

We’re talking about MEF not METH!!

Page 7: Extending the Enterprise with MEF

When to use MEF?When to use MEF?

» Only if you are building an extensible UI with Silverlight or WPF applications

NO!

Building extensible applications is one of the key nonfunctional requirements in enterprise application development scenarios.

.NET 4 for Enterprise Architects and Developers

Page 8: Extending the Enterprise with MEF

When to use MEF?When to use MEF?

» When you need a general purpose Dependency Injection framework or an Inversion of Control Container

NO!

We are not aiming for MEF to be an all-purpose IoC. The best way to think about the IoC aspects of MEF is an implementation detail. We use IoC as a pattern because it is a great way to address the problems we are looking to solve.

Glenn BlockMicrosoft

Page 9: Extending the Enterprise with MEF

When to use MEF?When to use MEF?

» MEF is especially useful for large applications where injecting the dependencies between distant parts would get hard to manage as the size of the code base increases.

MEF is focused on extensibility. When you think of MEF look at it as an investment in taking our platform forward. Our future products and the platform will leverage MEF as a standard mechanism for adding extensibility.

Imagine when you want to extend our platform in the future, you drop a DLL in the bin folder and you are done. The MEF enabled app lights up with the new extension. That's the vision for MEF.

Glenn BlockMicrosoft

YES!

Page 10: Extending the Enterprise with MEF

Benefits of being a MEF userBenefits of being a MEF user

» Decomposes large systems into building

blocks

» Allows extensibility across teams

» Turns your app into a platform

Page 11: Extending the Enterprise with MEF

Ready to get hooked Ready to get hooked on MEF?on MEF?

MEF Key ConceptsMEF Key Concepts

Page 12: Extending the Enterprise with MEF

MEF Key ConceptsMEF Key Concepts

Exports & Imports»Exports expose contracts for functionality that can be replaced at runtime. Exports are defined in your plugin libraries.»Imports are properties that consume Exported instances. Imports are defined in your host application.

Page 13: Extending the Enterprise with MEF

MEF Key ConceptsMEF Key Concepts

» ExportsDecorating a class with the Export attribute

The interface (IPlugin) should be in Contract Assembly that can be shared between the importer (host application) and the exporter

(plugin).

[Export(typeof(IPlugin)]public class MyPlugin : IPlugin{

}

[Export(typeof(IPlugin)]public class MyPlugin : IPlugin{

}

Page 14: Extending the Enterprise with MEF

MEF Key ConceptsMEF Key Concepts

» ImportsDecorating the host class with an ImportMany attribute

public class Host{ [ImportMany(typeof(IPlugin))] public IEnumberable<IPlugin> Plugins { get; set; }

...}

public class Host{ [ImportMany(typeof(IPlugin))] public IEnumberable<IPlugin> Plugins { get; set; }

...}

The interface (IPlugin) should be in Contract Assembly that can be shared between the importer (host application) and the exporter

(plugin).

Page 15: Extending the Enterprise with MEF

MEF Key ConceptsMEF Key Concepts

» ImportsExports can also define imports. For example, a plugin maybe dependent on a service from the host.

[Export(typeof(IPlugin)]public class MyPlugin : IPlugin{[Import]IService Dependency { get; set; }

}

[Export(typeof(IPlugin)]public class MyPlugin : IPlugin{[Import]IService Dependency { get; set; }

}

Page 16: Extending the Enterprise with MEF

MEF Key ConceptsMEF Key Concepts

Composition»Composition binds the Export with an Import at runtime to deliver the functionality.»A Catalog hosts the exports that need to be composed.»Types of catalogs:

» Assembly Catalog

» Directory Catalog

» Aggregate Catalog

» Type Catalog

Page 17: Extending the Enterprise with MEF

MEF Key ConceptsMEF Key Concepts

Composition»Loading a catalog and binding the exports to the imports:

var catalog = new DirectoryCatalog(@".\plugins");

var container = new CompositionContainer(catalog);

container.ComposeParts(host);

var catalog = new DirectoryCatalog(@".\plugins");

var container = new CompositionContainer(catalog);

container.ComposeParts(host);

Page 18: Extending the Enterprise with MEF

MEF Key ConceptsMEF Key Concepts

Composition»The Plugins collection now contains instances of all the exported types.

public class Host{ [ImportMany(typeof(IPlugin))] public IEnumberable<IPlugin> Plugins { get; set; }

...}

public class Host{ [ImportMany(typeof(IPlugin))] public IEnumberable<IPlugin> Plugins { get; set; }

...}

Page 19: Extending the Enterprise with MEF

MEF Key ConceptsMEF Key Concepts

Metadata»Custom metadata can be attached to your exported types to provide information to the importer.»Simply add an ExportMetadata attribute which accepts a key and a value:

[ExportMetadata(“ID”, 1)][Export(typeof(IPlugin)]public class MyPlugin : IPlugin{

}

[ExportMetadata(“ID”, 1)][Export(typeof(IPlugin)]public class MyPlugin : IPlugin{

}

Page 20: Extending the Enterprise with MEF

MEF Key ConceptsMEF Key Concepts

Metadata»In the importer, define an Interface to access the metadata. The property and data type must match the name/value pair specified in the ExportMetadata attribute.

public interface PluginMetadata{ int ID { get;set;}}

public interface PluginMetadata{ int ID { get;set;}}

Page 21: Extending the Enterprise with MEF

MEF Key ConceptsMEF Key Concepts

Metadata»Next, define the import to also load the metadata. Access both via .NET lazy load class.

public class Host{ [ImportMany] public IEnumerable<Lazy<IPlugin, IPluginMetadata>> Plugins { get; set; }

void UsePlugins() { foreach (var p in Plugins) { var id = p.Metadata.ID; var plugin = p.Value; } }}

public class Host{ [ImportMany] public IEnumerable<Lazy<IPlugin, IPluginMetadata>> Plugins { get; set; }

void UsePlugins() { foreach (var p in Plugins) { var id = p.Metadata.ID; var plugin = p.Value; } }}

Page 22: Extending the Enterprise with MEF

What’s new in .NET 4.5?What’s new in .NET 4.5?

» Support for generic types» Multiple scopes » Convention-based programming model that

enables you to create parts based on rules rather than attributes.

// Instead of using Export() attribute on Loggervar registration = new RegistrationBuilder();registration.ForType<Logger>().Export<ILogger>();

// Apply to all classes derived from ILoggerregistration.ForTypesDerivedFrom<ILogger>().Export<ILogger>();

// Instead of using Export() attribute on Loggervar registration = new RegistrationBuilder();registration.ForType<Logger>().Export<ILogger>();

// Apply to all classes derived from ILoggerregistration.ForTypesDerivedFrom<ILogger>().Export<ILogger>();

Page 23: Extending the Enterprise with MEF

Give me some MEF!Give me some MEF!

Demo!Demo!

https://github.com/jasongerard/MefExample

Page 24: Extending the Enterprise with MEF

Extending the Enterprise with MEFExtending the Enterprise with MEF

Brian RitchieChief ArchitectPaySpan, Inc.

Twitter: @brian_ritchieEmail: [email protected]: http://weblog.asp.net/britchieWeb: http://www.dotnetpowered.com

Thanks for Coming!

Jason GerardDirector of Product DevelopmentPaySpan, Inc.

Twitter: @thejasongerardEmail: [email protected]: http://jasongerard.wordpress.com


Recommended