Managed Extensibility Framework Explained
Managed Extensibility Framework (MEF) is a component of .NET Framework 4.0 aiming to create lightweight, extensible applications. It aims to allow .NET application developers to discover and use extensions with no configuration required. It also aims to let extension developers encapsulate code easily and avoid fragile hard dependencies. Furthermore, it aims to allow extensions to be reused across applications.[2] MEF was introduced as a part of .NET 4.0[3] and Silverlight 4. It was later improved with the release of .NET 4.5 by adding support for generic types and the introduction of a convention-based extension model.[4]
Overview
MEF aims to solve the runtime extensibility problem. Without MEF, any application that wants to support a plugin model needs to create its own infrastructure from scratch. Those plugins will often be application-specific and cannot be reused across multiple implementations.
- MEF aims to provide a standard way for the host application to expose itself and consume external extensions. Extensions, by their nature, could be reused amongst different applications. However, an extension can still be implemented in a way that is application-specific. Extensions themselves can depend on one another and MEF aims to make sure they are wired together in the correct order, sparing the developer from doing it manually.
- MEF offers a set of discovery approaches for the application to locate and load available extensions.
- MEF allows tagging extensions with additional metadata which aims to facilitate rich querying and filtering.
Design
Roughly speaking, MEF's core consists of a catalog and a CompositionContainer. A catalog is responsible for discovering extensions and the container coordinates creation and satisfies dependencies.
- MEF's first-class citizen is the ComposablePart class. A composable part offers up one or more Exports, and may also depend on one or more externally provided services or Imports. A composable part also manages an instance, which can be an object instance of a given type (it is in the default MEF implementation). MEF, however, is extensible and additional ComposablePart implementations can be provided as long as they adhere to the Import/Export contracts.
- Exports and imports each have a Contract. Contracts are the bridge between exports and imports. An export contract can consist of further metadata that can be used to filter on its discovery. For example, it might indicate a specific capability that the export offers.
- MEF's container interacts with Catalogs to have access to composable parts. The container itself resolves a part's dependencies and exposes Exports to the outside world. Composable part instances may be added directly to the container.
- A ComposablePart returned by a catalog will likely be an extension to the application. It might have Imports (dependencies) on components the host application offers, and it's likely to Export others.
- The default MEF composable part implementation uses attribute-based metadata to declare exports and imports. This allows MEF to determine which parts, imports, and exports are available through discovery.
Further reading
- Book: Jeremy. Likness. 2011. Fundamentals of the Managed Extensibility Framework (MEF). Addison-Wesley. 978-0132929400.
- Book: Rishabh. Verma. 2020. Visual Studio Extensibility Development: Extending Visual Studio IDE for Productivity, Quality, Tooling, and Analysis. Apress. 978-1484258538.
- Book: Alan. Harris. 2010. Pro ASP.NET 4 CMS: Advanced Techniques for C# Developers Using the .NET 4 Framework. Apress. 978-1430227137.
- Book: Christian. Nagel. Bill. Evjen. Jay. Glynn. Karli. Watson. Morgan. Skinner. 2010. Professional C# 4.0 and .NET 4. John Wiley & Sons. 978-0470632536.
External links
Notes and References
- Web site: MEF. . 15 October 2021.
- Web site: How to work with the Managed Extensibility Framework in C#. Kanjilal. Joydip. 7 July 2016. InfoWorld. 25 June 2022.
- Web site: What's New in VS2010 and .Net 4?. Pronschinske. Mitch. 12 April 2010. DZone. 25 June 2022.
- Web site: Managed Extensibility Framework Improvements in .NET 4.5. Vogel. Eric. 12 February 2010. VisualStudio Magazine. 25 June 2022.