MEF basic concepts learning notes, mef concepts learning notes
MEF is a framework class library under the Microsoft. net Framework. It can make your program load expansion with low coupling. It is often used when developing plug-ins or developing functions that require flexible extension. For example, the calculator example provided by Microsoft. When you develop a calculator, the initial function only provides the addition and subtraction function. But then you need to extend the multiplication and division functions. Obviously, modifying the entire program will cause problems and unpredictable problems. Therefore, Microsoft provides us with MEF to add new features to programs by dynamically loading extensions. In addition, mef can also be used to implement dependency injection and control inversion.
Let's start with the simplest DEMO to learn about mef.
using System;using System.Collections.Generic;using System.ComponentModel.Composition;using System.ComponentModel.Composition.Hosting;using System.Reflection;namespace MEFDEMO{ class Program { [Import] public string Message { get; set; } static void Main(string[] args) { Program program = new Program(); program.Run(); } public void Run() { var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); var container = new CompositionContainer(catalog); container.ComposeParts(this); Console.WriteLine(Message); Console.ReadKey(); } } public class SimpleHello { [Export] public string Message { get { return "Hello World!"; } } }}
This is the simplest example. From the above we can see that a simple mef implementation consists of the following parts:
1. Entry for calling a program[Import] public string Message {get; set ;}
2. Implementation of methods[Export] public string Message {get {return "Hello World! ";}}
The specific implementation of the method is what we can dynamically add. Compared with general interface implementation, MEF makes it more flexible and scalable. Dynamically add
3. Combine the above two methods to establish the relationship between them.
Var catalog = new AssemblyCatalog (Assembly. GetExecutingAssembly ());
Var container = new CompositionContainer (catalog );
Container. ComposeParts (this);
System. componentModel. composition. hosting. aggregateCatalog category = new System. componentModel. composition. hosting. aggregateCatalog (); foreach (var file in System. IO. directory. getFiles (@ "C: \ Users \ wfm \ Documents \ Visual Studio 2013 \ Projects \ WPFTest \ TestReference \ bin \ Debug") {if (file. endsWith (". dll ", StringComparison. invariantCultureIgnoreCase) {try {var assembly = Assembly. loadFile (file); if (assembly = Assembly. getCallingAssembly () continue; category. catalogs. add (new System. componentModel. composition. hosting. assemblyCatalog (assembly);} catch {}} Container = new CompositionContainer (category, true); this. container. composeParts (this );
Var container = new CompositionContainer (catalog );
This Code creates a container. It is easy to create a container. You can pass in a ComposablePartCatalog class or its subclass. You can also pass in the ExportProvider class, depending on the method overload.
Container. ComposeParts (this );
Here ComposeParts is an extension method. The first parameter is omitted during the call. Therefore, the input this, type: System. Object [] is an array of the property objects to be combined. That is to say, pass the [Import] public string Message {get; set;} here, and then the container will find the export object that matches the required Import object.
In this example, we use container. ComposeParts (this); for combination import and export. But there are other methods. For example:
Container. SatisfyImportsOnce (this );
Container. Compose (batch );
T instance = Container. GetExportedValue <T> (); then it is called through the instance.
Address: http://www.cnblogs.com/santian/p/4355730.html
Blog: http://www.cnblogs.com/santian/
For reprinting, please use hyperlinks to indicate the original source of the article.