Beginner's Guide to Managed Extensibility Framework

06 August 2021 at 10:00 by ParTech Media - Post a comment

Managed Extensibility Framework (MEF) was a new feature of.NET 4.0. It allows developers to design apps that can be dynamically extended and reused across numerous applications.

MEF enables application developers to locate and use extensions without the need for configuration. It also makes it simple for extension authors to encapsulate code and eliminate hard dependencies. In this post, we will understand everything about MEF, including what it is and how does it work.

Table of contents

  • What is Managed Extensibility Framework?
  • Details about Managed Extensibility Framework
  • Managed Extensibility Framework Nomenclature
  • How does Managed Extensibility Framework (MEF) work?
  • Conclusion

What is Managed Extensibility Framework?

The MEF (Managed Extensibility Framework) is an included component. By using a loosely coupled plugin-like architecture, Net Framework 4.0 (and later) enables you to create lightweight and extensible applications. This framework can be used to discover and use extensions without the need for any configuration.

MEF makes it extremely simple to increase the flexibility, maintainability, and testability of your applications. You can reuse extensions within the same application or even across applications while using MEF. MEF can also be considered a library for creating lightweight and extensible applications. It also makes it simple for extension developers to encapsulate code and avoid hard dependencies.

Managed Extensibility Framework Nomenclature

Before we get started with the working of Managed Extensible Framework, let's have a look at some of the essential terms that will help us understand it much better -

  • Part: A Part is an object (such as a class, function, or property) that can be exported by a plugin and imported by a program.
  • Catalog: A catalog is an item that aids in the discovery of composable parts from an assembly or directory.
  • Contract: The imported and exported pieces must communicate with one another via a contract (e.g. an Interface or predefined data type like string)
  • Attribute of Import: It has the function of a Hook or a Socket. It defines a part's requirement and it only applies to a single Export Attribute.
  • Import: Many Attribute is identical to Import Attribute, except it allows for numerous Export Attributes to be imported.
  • Export Attribute: This attribute acts as a plug that can be connected to the Socket (i.e. Import) to complete the flow. The import property is what causes the requirement. The Export attribute satisfies this need. It implies that such elements will be included in the composition.
  • Compose: In MEF terms, compose is the space where the exported and imported components connect and where the exported and imported parts are assembled.

How does Managed Extensibility Framework (MEF) work?

Managed Extensibility Framework provides an implicit means for an application to discover external components whenever the need to use them arises. The ComposablePart component is a MEF component. It enumerates all of the capabilities that the ComposablePart has to offer.

The Import specifies the part's interdependencies with other components. These ComposableParts are extensions that can be discovered at runtime. A MEF-based extensible application declares imports that can be filled by extension components and exports that expose services to extensions. Each extension component declares an Export as well as Imports if applicable.

The CompositionContainer, which holds all of the ComposableParts and conducts composition, is at the heart of the MEF composition paradigm (connecting imports to exports). It is also the most frequently used CompositionContainer.

Now let us get into the working of MEF. The suppliable component of the Managed Extensible Framework should be discovered. The contract must be signed by both the suppliable and the required parts (i.e Interface). MEF performs a dynamic discovery of those parts into Directory or Assembly from a random location whenever any parts are required to be plugged into the application (i.e: Catalog). As long as the contract is met, the composition engine (i.e. Composition Container) will combine those pieces together.

Let us understand this better through diagrammatic representation:-

Composition container, parts, and catalogs

The essential building components of a MEF application are parts and the composition container. A component is an object, up to and including itself, that imports or exports a value. A catalog is a collection of parts from a single supplier. To execute composition, the composition container uses pieces provided by a catalog and the binding of imports to exclusions.

Imports and exports

The way components communicate is through imports and exports. The component expresses a need for a specific value or object with an import, and it specifies the availability of a value with an export. By way of its contract, each import is matched with a list of exports.

Conclusion

Many of the architects and developers have been using MEF in their architecture and it has allowed them to create more composite, extendable, tested, and scalable applications faster and better. So what are you waiting for? Go ahead and use this in your organization to power up your applications.

Latest