DOTNET 4 Study Notes 1-----mef (managed extensibility Framework)

Source: Internet
Author: User
Tags dotnet
Document directory
  • MEF (managed extensibility Framework)
  • How to Create a MEF Application
  • Practical Application
  • Summary

. Net 4 and the corresponding vs2010 have come out for a while. From the earliest beta1 to today's RC, it has become closer to the official version.

Recently I had nothing to worry about, and it seems that some holiday syndrome is too much, so I calm down and study DOTNET 4.0 from the beginning.

First, let's study MEF.

MEF (managed extensibility Framework)

Generally, due to frequent changes in application requirements, the scalability of application software is getting worse and it is difficult to add new functions. in net4.0, MEF (managed extensible framework) was added to solve the problem of simplifying the design of extensible applications and components.

Since MEF is already included in. Net 4.0, we do not need to download its class library. Of course, if you are interested in its implementation principles, you can check its source code at http://www.codeplex.com/mef.

 

How to Create a MEF Application

Generally, the following steps are required to create a MEF application.

  1. Use the [import] attribute to mark extensible areas and express them as interfaces
  2. Create an extended component and use the [Export] flag when implementing the (1) Interface

 

Next we will use a simple example to do the above two steps.

First, we declare an extended attribute message and implement the do () method when calling it ().

Note that the import attribute is under system. componentmodel. Composition. You need to introduce system. componentmodel in the project.

Class meftest
{
[Import]
Public String message {Get; set ;}
Public void compose ()
{
Compositioncontainer Container = new compositioncontainer ();
Compositionbatch batch = new compositionbatch ();
Batch. addpart (New extension1 ());
Batch. addpart (this );
Container. Compose (batch );
}
Public Void Do ()
{
Console. writeline (Message );
Console. readkey ();
}
}

 

Next, we need to implement a specific extended object.

Class extension1
{
[Export]
Public String message
{
Get
{
Return "I am extension 1 ";
}
}
}

In this way, before we need to formally initialize the meftest instance and call its do method, we naturally need a method to "bind" The extensible message object to the message interface. This is the role of the compose () method. Among them, a compositioncontainer container is created to bind this attribute.

Note that the corresponding import must be included in the container for any export mark, but you can use compose () instead ().

Practical Application

Generally, it is not as simple as the above. MEF supports binding extensible objects by means of an assembly, directory (directorycatalog), or a combination of two.

Compositioncontainer Container = new compositioncontainer (

New directorycatalog (path. getdirectoryname (assembly. getexecutingassembly (). Location )));

If you use directorycatalog when creating a compositioncontainer container, all the assemblies in the target path will be traversed and queried to find the specific class that implements the import interface.

Of course, use [Export (typeof (Interface)] to precisely locate the export.

In addition, in general use, we need to use some specific metadata (metadata) to further describe or describe some specific interfaces (such as security and speed ). We can use partmetadata to mark the class and use exportmetadata to mark the implementation method.

 

Summary

This article mainly introduces MEF and its simple application. Hope that the majority of users correct.

To be continue ....

 

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.