Overview
The Managed Extensibility Framework (MEF) is an extensibility management framework under the. NET platform that is a collection of features, including dependency injection (DI) and duck typing. MEF provides developers with a tool that allows us to easily extend the application and have minimal impact on existing code, as developers define some extension points based on functional requirements during development, and then they can use these extension points to interact with the application At the same time, the MEF makes the application not directly dependent on the Extender, which allows the extension to be shared among multiple extended requirements.
Simple Dependency Injection
You can go here. HTTP://CODE.MSDN.MICROSOFT.COM/MEF downloads the CTP version of MEF, with a few simple documents and examples in the download package. Let's take a look at a simple example and output a string here:
Code 1
// Author: TerryLee 2008.8.30
// URL: http://www.cnblogs.com/Terrylee
static void Main(string[] args)
{
Console.WriteLine("This is a simple string.");
}
Now that the string may change in the future, and I don't know where the string will be taken from, meaning there might be a change point and an extension point, then we're going to use MEF to redesign it, and we'll split the process into two parts, part of it to provide strings, And the other part uses strings, defines a string provider, and you notice that an export attribute is added to the Outputtitle property, which identifies this as an output that enables the MEF to recognize it, as shown in the following code:
Code 2
// Author: TerryLee 2008.8.30
// URL: http://www.cnblogs.com/Terrylee
public class WeekStringProvider
{
[Export("Caption")]
public String OutputTitle
{
get { return "星期六"; }
}
}
Here just defines a simple attribute, in fact, in the same type can define multiple outputs, export also specifies a string of the contract name, which means that any matching contract name of the program can use the extension. In addition, we can specify a type to replace the contract name of the string, as we'll say later. We define an input, which is used to consume the string, as well as a simple attribute, but this time we add the import feature:
Code 3
// Author: TerryLee 2008.8.30
// URL: http://www.cnblogs.com/Terrylee
public class Client
{
[Import("Caption")]
public String OutputTitle { get; set; }
}