MEF programming guide 1: Host MEF and mef programming in applications

Source: Internet
Author: User

[Transfer] MEF programming guide 1: Host MEF and mef programming in applications

In the application, the host MEF is actually very simple. You only need to create an instance of the CompositionContainer, and then combine the components (Parts) to be combined) and the current host Program can be added to the container. First, you must add references to the MEF framework, namely System. ComponentModel. Composition. dll. The detailed code block is as follows:

Private void Compose ()
{
Var catalog = new AssemblyCatalog (Assembly. GetExecutingAssembly ());
Var container = new CompositionContainer (catalog );
Container. ComposeParts (this );
}

 

Through the above Code implementation, you can complete the MEF host. In fact, it is not so simple to use in the development process of MEF. One or more Import and Export parts may be defined, and then combined through the MEF container. In fact, it can also be understood as an implementation of "dependency injection. For example, define a library interface and an interface implementation class, and then use the Import and Export features of MEF:

Public interface IBookService
{
Void GetBookName ();
}

/// <Summary>
/// Import
/// </Summary>
[Export (typeof (IBookService)]
Public class ComputerBookService: IBookService
{
Public void GetBookName ()
{
Console. WriteLine ("Hello Silverlight");
}
}

 

The above Code uses the [System. componentModel. composition. export] to Export the implementation of the interface, so that the implementation of the interface exists as a container component, and then assemble and load by combining the container, this process includes the interface instantiation process. Next, you need to define an interface attribute in the MEF Host Program and mark it with the [System. ComponentModel. Composition. Import] feature to implement class Import for the interface. The following code block:

/// <Summary>
/// Part of the import Interface)
/// </Summary>
[Import]
Public IBookService Service {get; set ;}

 

After completing the development and feature configuration of the Import and Export interfaces and implementations, the next step is to combine the components and the Host Program itself into the combined container, import and Export.

/// <Summary>
/// Host MEF and combine parts
/// </Summary>
Private void Compose ()
{
Var catalog = new AssemblyCatalog (Assembly. GetExecutingAssembly ());
Var container = new CompositionContainer (catalog );
// Add the part and Host Program to the combined container
Container. ComposeParts (this, new ComputerBookService ());
}

 

Through the above steps, the MEF host and an application example of a simple component combination are completed. The complete code example in this article is as follows:

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. ComponentModel. Composition;
Using System. ComponentModel. Composition. Hosting;
Using System. Reflection;

Namespace HostingMef
{
Public interface IBookService
{
Void GetBookName ();
}

/// <Summary>
/// Import
/// </Summary>
[Export (typeof (IBookService)]
Public class ComputerBookService: IBookService
{
Public void GetBookName ()
{
Console. WriteLine ("Hello Silverlight");
}
}

Class Program
{
/// <Summary>
/// Part of the import Interface)
/// </Summary>
[Import]
Public IBookService Service {get; set ;}

/// <Summary>
/// Host MEF and combine parts
/// </Summary>
Private void Compose ()
{
Var catalog = new AssemblyCatalog (Assembly. GetExecutingAssembly ());
Var container = new CompositionContainer (catalog );
// Add the part and Host Program to the combined container
Container. ComposeParts (this, new ComputerBookService ());
}

Static void Main (string [] args)
{
Program p = new Program ();
P. Compose ();

P. Service. GetBookName ();
}
}
}

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.