⬆️ ⬇️

Microsoft Managed Extensibility Framework

On the fourth of June, the Microsoft development team presented the CTP project Managed Extensibility Framework (MEF). MEF is a mechanism that allows a minimum of code to embed extensibility support in projects (for example, plug-ins).



Link to the message from the developers: weblogs.asp.net/whaggard/archive/2008/06/04/first-managed-extensibility-framework-mef-bits-released.aspx



Download CTP along with examples and documentation here: code.msdn.microsoft.com/mef

Further I will give a short example from the documentation for a better understanding of what MEF is.



So what does MEF give? Consider the first example of the project documentation. The first piece of code describes a simple property that affects the button header.

[Import ( "ButtonCaption" )]

public String ButtonCaption

{

get { return theButton.Content.ToString (); }

set {theButton.Content = value ; }

} * This source code was highlighted with Source Code Highlighter .


')

Note that this property is marked with the [Import ("ButtonCaption")] attribute. It is, in its own way, a mark of where the data can be changed through external extensions.



The following defines the simplest extension:

public class ExampleStringProvider

{

[Export ( "ButtonCaption" )]

public String providedCaption

{

get { return "MEF Hello World !!" ; }

}

} * This source code was highlighted with Source Code Highlighter .




As can be seen from the code, the class defines a data source through [Export (“ButtonCaption”)], which indicates that this class is able to set the header values ​​for the button.



In the end, both pieces of code are mixed in order to meet each other:

public MyHelloWorld ()

{

Initializecomponent ();

CompositionContainer container =

new CompositionContainer ();

container.AddComponent <MefHelloWorld.MyHelloWorld> ( this );

container.AddComponent ( new

ExampleStringProvider ());

container.Bind ();

} * This source code was highlighted with Source Code Highlighter .



This code registers the interconnected components in a container (composition container) and, by calling the Bind method, binds the data from the extension to the existing property. Alternatively, the class of the ExampleStringProvider extension can be replaced by another class that provides other functionality, for example, which instead of text displays the date:

public class DateStringProvider

{

[Export ( "ButtonCaption" )]

public String providedCaption

{

get { return DateTime .Today.ToString (); }

}

} * This source code was highlighted with Source Code Highlighter .




As can be seen from a simple example, MEF implements a mechanism to bind data during the execution of a code through a reflection mechanism. The idea lay on the surface, and I am glad that Microsoft took care to create a convenient and powerful library that implements the mechanism for expanding the functionality of the code.

Source: https://habr.com/ru/post/26853/



All Articles