Battlefield MEF (4): Search scope

Source: Internet
Author: User

In the previous article, almost every example we had access to the search location of the extension class, we might as well think of, since it is automatic extension, it will certainly have one or more people to find the location, otherwise the MEF framework how to know where there are extension components?

Just as we use the navigation system to find all the hotels in a certain place, just because the hotel has registered information in the database, our navigation system can find it, if a hotel does not provide any data to the navigation database, it is obvious that the navigation system is not able to identify its detailed address.

There are typically three MEF lookup scopes for extension components:

    1. Assemblycatalog: Find from an assembly.
    2. Applicationcatalog: Look in the directory where the application is located.
    3. Directorycatalog: Find in a directory, such as D:\\libs.

If you are not enough, you can use Aggregatecatalog to set multiple lookup scopes.

Let's try it one by one.

    1. Assemblycatalog

Based on a range lookup for an assembly, the most common thing to do is to find it in the current assembly, or to find it in other assemblies, of course. As long as you can get a assembly object can be found in it, everyone is so smart, I believe you can understand.

The following is a typical example of finding an extension part in the current assembly.

First, we define an interface for standardizing the behavior of extended parts.

Then, write two classes to implement the above interfaces and export them.

In the application class, import all of them with a common field.

Finally, assemble in main and test the call.

Finally, the program runs as shown in the results.

Second, Directorycatalog

This one we may be a little strange, because in the previous article in the example, we have not used the class, but not because of the strange and afraid, how high the sky, your courage should be how big, unfamiliar things are often not complex, as if we talk to strangers sometimes more comfortable than dialogue with acquaintances.

The Directorycatalog class can specify a directory where MEF looks for extensions under this directory, and if more detailed lookups are needed, you can specify a search string in the constructor of the Directorycatalog class that is the same as we normally search for files, such as "Ext_*", which indicates that class library files starting with Ext_ are in the look-in range.

This time, we use a WinForm application to do an example, in the form we drag and drop a few controls, see.

Control layout at will, I have always advocated personalized layout, the main following a few important controls to introduce:

A textbox named Txtdirec is used to display the selected directory, and a ComboBox control named Cmbclass is used to display the list of extension classes that MEF discovers, and a textbox named Txtresult displays the result of the extension class's invocation.

The following is a general idea of this example, we first put the extended class library into an arbitrary folder, and then select the directory on the application window, let the MEF framework find all the extension types in the directory and display in the ComboBox. We then select a class from the ComboBox to invoke and view the result of the call.

    1. We first build a public class library, which is referenced in both the main application and all extension class libraries, because we define the "Constitution" (interface) that is used to standardize the behavior of the component. This class library is named Commonlib, the code is as follows, very simple, no more than 10 lines.

This interface has only one method saysomething, we will use a few classes to implement it, each class is written independently of a class library.

    1. New Item A class library project, named Extliba, with the following code:

Similarly, two other class library projects were built, namely Extlibb and EXTLIBC.

    1. Generate these three DLLs separately, and also include a CommonLib.dll, because three extension libraries reference it. Then put these DLLs into a folder, roughly as shown.

    1. Back to the WinForm project, declaring a field in a form class, either public or private, is a matter of importing a type.

    1. Handles button events, browses the directory, and looks for extension parts from the specified directory, and appears in the ComboBox.

    1. Handles button events, calling the extension type selected in Comobox.

We can then look at the results of the execution below.

Third, Aggregatecatalog

One day, I accidentally wrote the new extension class extlibd to the WinForm project, like this:

Then I think it would be nice if I could keep the previous three extension classes and also automatically identify the extension classes I wrote to the WinForm project. Afraid. NET is powerful only you can't imagine, no it can't do. At this time I looked at the Aggregatecatalog class, using it to merge multiple search scopes, the three extensions we just made were searched in a folder, and the fourth extension was written to the WinForm project, Remember the previous article we said about the Assemblycatalog class, which can look for extension classes in an assembly, so the fourth extension class can be found in the assembly where the WinForm project is located.

In other words, we combine the Assemblycatalog range and the Directorycatalog range. So, change the code in front button1_click to look like this:

Using Aggregatecatalog to combine Directorycatalog and Assemblycatalog, and then to Compositioncontainer's constructor, you can implement a combination of lookup scopes.

OK, now run it and see if the fourth extension is automatically discovered?

Good, the expected effect achieved, is not very happy, drink a cup of coffee proud of it.

I do not know that you have read my several of the MEF's bragging prose, will not sigh the magic of the MEF? No matter where you're going to use MEF, just remember the two principles:

    1. It is necessary to act decisively when a technology is needed, or else to be troubled by it.
    2. The use of technology is flexible, no recruit wins, like Bruce Lee's "Lie Triple foot", avoid abuse.

Article reproduced: East Evil Solitary

Battlefield MEF (4): Search scope

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.