Managed EXTENSIBILITY Framework: Focused on Extensibility
Every system while being designed considers the growth prospects. Extensibility is a measure of the ability of a system to extend itself and how much efforts will be required to bring about the desired change. The possible changes in the system could be either the inclusion of a new functionality or modifications in the existing code.
Let me describe this through a project scenario. The software plans to target various industry sectors for their courier and transportation management and hence should be flexible to fulfill requirements of different sectors. This means with every new business sector connecting with the client, there might arise a need to extend the system with new components.
The possible solutions could be:
- To include this component directly as source code in your application and call it directly from your code.
- To provide an interface to implement new components. An API then has to be developed to interact with the application for decoupling between application and its components.
The above solutions bring along few drawbacks:
- The source code needs to be changes.
- In-case of adding third party components, the source code is not allowed to be accessed by them.
- A separate configuration is required to discover the added components.
- The various components added are unable to communicate with each other.
- Due to hard dependency on the interface, the components cannot be used in other applications.
Here comes MEF to resolve all the issues. Managed Extensibility Framework is basically incorporated in .NET Framework to build lightweight, extensible applications. It allows developers to discover extensions easily and use them without any configuration. It lets the developers develop encapsulated code and avoid any sort of hard dependencies. The components can then be used easily in other applications as well.
There are few terminologies related to MEF that needs to be understood to gulp in the working concept of MEF. These are:
- Import means receiving some service.
- Export means providing some service.
Part refers to a class, method or property that has to be imported or exported in the application.
- Catalog discovers the available parts from the specified source. The source could be a directory or an assembly, etc.
- The contract refers to an agreement between an imported and an exported part.
Compose refers to the area where all imported and exported parts agreeing to the contract are assembled
Understanding the working of MEF
The master of MEF is a Composition Container where all the exported and imported parts agreeing to the contract are loaded. The catalog helps in loading these parts from the specified source. With an import, the component specifies a need for a particular value or object and with an export it specifies the availability of a value. During runtime, the import is dynamically bound with the export on the matched value. The imports are decoupled from exports’ implementation.
MEF is not by any means the first solution for this issue. Several solutions have been proposed earlier like EJB, CORBA, Spring on Java side, System. Addin in .NET, etc. But these solutions are not successful for third-party extensibility and require huge efforts from developers’ end to inculcate them into his application.
Advantages of MEF
- Since it is a part of .NET framework, the developer need not install any additional tools. Also, there are no version compatibility issues.
- No configuration required to include new components into the application. Further, these components can be included in other applications as well. Thus MEF promotes reusability.
- With MEF, you can be assured that your application can be safely extended in real time.
- It allows you to split your application into various modules that can be loaded on demand.
- It helps in dynamic discovery of the components.
- It breaks the tightly coupled dependencies in the project but respects the type checking of loosely coupled parts.
- It allows other developers to create plugins for your application. Without having any access to your code or changing the source, the other users can extend your application and modify its working.
Advantages of MEF in above-discussed project
Apart from the above benefits provided by MEF in general, it helps to meet the client demands for a new business component to be incorporated in the project in significantly less time. The application is perfectly extensible with dynamic loading of required .dll files in the code based on the requirement. MEF is easy to understand and can be implemented easily without any major changes in your existing code. You just need to code the new component, everything else is handled by MEF.
So with MEF, let’s move more into a world of Dynamic Coding and easily Extensible Applications. Easy to use and to reuse!!