MEF Programming Guide (first two sections)

Source: Internet
Author: User

 

MEF Programming Guide (first two sections)

Use MEF in applications

To use MEF in an application, you need to create a compositioncontainer instance, add components that can be combined to it, include the Host application, and then combine it.

The following steps are required to use MEF:

1. Create a host class. In the following example, we will use a console application, so the host is the program class.

2. reference the system. componentmodel. Composition assembly.

3. Add the following using statement: using system. componentmodel. composition;

4. Add a compose () method, which creates a container instance and works in combination.

5. Add a run () method, which calls the compose () method.

6. instantiate the host class in the main () method.

Note: This step is not required in ASP. NET and WPF, because the host class is initialized at runtime.

The following code demonstrates the code style.

 Using system. componentmodel. composition; <br/> using system. componentmodel. composition. hosting; <br/> using system. reflection; <br/> using system; <br/> public class Program <br/> {<br/> Public static void main (string [] ARGs) <br/>{< br/> program P = new program (); <br/> P. run (); <br/>}< br/> Public void run () <br/>{< br/> compose (); <br/>}< br/> private void compose () <br/>{< br/> var Container = new compositioncontainer (); <br/> container. composeparts (this); <br/>}< br/>} 

7. Define one or more hosts to import (exports ). In the following code, we will create an interface called imessagesender. I will also define a composable component -- emailsender class, which exports an imessagesender by using the [system. componentmodel. Composition. Export] feature.

<Br/> Public interface imessagesender <br/>{< br/> void send (string message ); <br/>}< br/> [Export (typeof (imessagesender)] <br/> public class emailsender: imessagesender <br/>{< br/> Public void send (string message) <br/>{< br/> console. writeline (Message); <br/>}< br/>} 

8. Add attributes to the host class. Each attribute is modified by [system. componentmodel. Composition. Import. The following is an import of the imessegesender type added to the program class.

 [Import] <br/> Public imessagesender messagesender {Get; set ;} 

9. Add composite parts to the container. There are multiple ways to add composite parts to a container in MEF. One of them is to directly add an instance that can be combined, and another more common method is to use a directory (Catalog), which will be explained later.

Add components directly to the container

You can use the composeparts () method to manually add composite components in the compose () method. In the following example, an emailsender instance and an instance of the program class that needs to be imported are added to the container.

<Br/> private void compose () <br/>{< br/> var Container = new compositioncontainer (); <br/> container. composeparts (this, new emailsender (); <br/>} 

Use assemblycatalog to add composite components to the container

By using catalog, containers can automatically create component instances without explicitly adding them. Create a catalog in the compose () method. Then it is passed into the constructor of the container.

In the following example, an assemblycatalog is created by passing the current Assembly into its constructor. We have not manually added the emailsender instance, and it will be automatically discovered.

<Br/> private void compose () <br/> {<br/> var catalog = new assemblycatalog (system. reflection. assembly. getexecutingassembly (); <br/> var Container = new compositioncontainer (catiner); <br/> container. composeparts (this); <br/>} 

After completing the above steps, the code should be the following style.


Using system. componentmodel. composition; <br/> using system. componentmodel. composition. hosting; <br/> using system. reflection; <br/> using system; <br/> public class Program <br/> {<br/> [import] <br/> Public imessagesender messagesender {Get; set ;}< br/> Public static void main (string [] ARGs) <br/>{< br/> program P = new program (); <br/> P. run (); <br/>}< br/> Public void run () <br/>{< br/> compose (); <br/> messagesender. send ("message sent"); <br/>}< br/> private void compose () <br/>{< br/> assemblycatalog catalog = new assemblycatalog (assembly. getexecutingassembly (); <br/> var Container = new compositioncontainer (catiner); <br/> container. composeparts (this); <br/>}< br/> Public interface imessagesender <br/>{< br/> void send (string message ); <br/>}< br/> [Export (typeof (imessagesender)] <br/> public class emailsender: imessagesender <br/>{< br/> Public void send (string message) <br/>{< br/> console. writeline (Message); <br/>}< br/>} 

When the code above is compiled and run, the application and the import it requires will be combined. The send () method will be called to output "message sent" in the console ".

Define composite parts and contracts

Composite parts

You can combine components to export services required by other components, or import services provided by other components. In MEF, components that can be combined must be imported and exported using system. componentmodel. Composition. Import and system. componentmodel. Composition. Export. A component that can be combined should contain at least one export. Components that can be combined may be explicitly added to the container or created by using catalog. When MEF is released, the default catalog can be used to identify components that can be combined through the export feature.

Contract

Components that can be combined are not directly dependent on each other. They all depend on a contract, that is, a tag string. Each export has a contract, and the import needs to declare which contract it needs. The container uses the contract information to match the import and export operations. If no contract is specified, MEF uses the full qualified name of the type as the contract by default. If a type is input in the export, MEF uses a fully qualified name.

All the export contracts in the following code are equivalent.

Namespace mefsample <br/>{< br/> [Export] <br/> public class exporter {...} <br/> [Export (typeof (exporter)] <br/> public class exporter1 {...} <br/> [Export ("mefsample. exporter ")] <br/> public class exporter2 {...} <br/>} 

Interface/ABSTRACT Contract

Generally, a component that can be combined is exported by an interface or abstract type, rather than a specific type. For example, in the following code, both classes export imessagesender. The notifier class imports a group of imessagesender and calls the send () method for each item. Now the new information transmitter can be easily added to the system.

<Br/> [Export (typeof (imessagesender)] <br/> public class emailsender: imessagesender {<br/>... <br/>}< br/> [Export (typeof (imessagesender)] <br/> public class tcpsender: imessagesender {<br/>... <br/>}< br/> public class notifier {<br/> [importplugin] <br/> Public ienumerable <imessagesender> senders {Get; set ;} <br/> Public void Policy (string message) {<br/> foreach (imessagesender sender in senders) <br/> sender. send (Message); <br/> }} 

 

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.