MEF is easy to get started with, but it is hard to understand (1 ).

Source: Internet
Author: User
Tags processing text

MEF is easy to get started with, but it is hard to understand (1 ).

Cause: work requires different processing for different types of files. For example, a txt file can be printed directly, a doc file can be used, and an email or jpg file can be sent directly to the xxx album site.

In fact, this problem has already been solved when learning the most basic factory model. For a little object-oriented, you can write such a file manager. When processing new types of files, you only need to add an implementation class, and then add an if judgment in the factory to return a specific processing instance. The upper layer does not need to be modified.

To get rid of the if in the factory, you must get out of the ioc container. MEF is Microsoft's self-hosted scalable framework. Here I use it as an ioc container. Other functions are not very easy to understand.

First, we use the MEF trainer and then apply it to a specific project. Concept or something should be put aside first.

Create a console application project and a class library project. Both projects reference the MEF library. Create three new class files in the class library project.

Using System; using System. componentModel. composition; namespace Parts {[Export (typeof (object)] // indicates that this class needs to be exported. The Export type is object public class TxtFileHandler {public void Process () {Console. writeLine ("processing text files ");}}}

The rest will not be pasted, but the class name and output statement are different.

Main Program:

Using System; using System. ComponentModel. Composition. Hosting; namespace meftest {class Program {// container, used for loading. No matter what you install. Private static CompositionContainer container; static void Main (string [] args) {// AssemblyCatalog Directory, which indicates searching var assemblyCatalog = new AssemblyCatalog (typeof (Program) in the Assembly ). assembly); // This sentence is actually useless, because this Assembly does not have any instances (various handler) We need. // you can search for the dll in a directory. Var directoryCatalog = new DirectoryCatalog (AppDomain. currentDomain. baseDirectory ,"*. dll "); var aggregateCatalog = new AggregateCatalog (assemblyCatalog, directoryCatalog); // create a component and place it in the container. Container = new CompositionContainer (aggregateCatalog); var exports = container. GetExports <object> (); // obtain all exported parts (object type ). Foreach (var item in exports) {Console. WriteLine (item. Value) ;}console. ReadLine ();}}}

Compile two projects and copy the generated class library file Parts. dll to the bin \ debug folder of the main program.

Run the main program:

As you can see, the class name (object. ToString () is printed ()). We have successfully created three class instances, but the main program does not reference this class library.

We can also say that the class instance is successfully injected into the main program.

From this small example, we can learn to use MEF in three steps: 1. Export the desired type (component), 2. Search in the appropriate directory (AssemblyCatalog, DirectoryCatalog. 3. Add the found parts to the container.

Then you can use the New instance in the container.

Note:

The mark [Export (typeof (object)] on the TxtFileHandler class indicates that this class needs to be exported and the Export type is object.

In the main function, var exports = container. GetExports <object> (); // obtain all exported parts (object type ).

The exported type must be the same as the type to be obtained. Such consistency is called a contract.

I hate a lot of articles in the world. Please do not repost them.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.