MEF programming guide 9: Recomposition MEF parts

Source: Internet
Author: User

Based on the previous eight programming guidelines, I believe everyone knows that the combination of application components is assembled during initialization. If the application Initialization is complete and new components are imported and assembled, the current implementation method cannot be used, we need the Recomposition function provided by MEF to dynamically assemble, dynamically combine, and dynamically send notifications of new component assembly combinations) component features can be used.

 

Here we use the example of the application in the previous article as a function demonstration scenario. Three different implementation classes under a library interface are defined in the main program. The detailed code segment is as follows:

Namespace MEFTraining. Recomposition
{
[Export (typeof (IBookService)]
Public class MEFBookService: IBookService
{
Public string GetBookName ()
{
Return "MEF programming guide";
}
}

[Export (typeof (IBookService)]
Public class ASPNETBookService: IBookService
{
Public string GetBookName ()
{
Return "ASP. NET project case";
}
}

[Export (typeof (IBookService)]
Public class SLBookService: IBookService
{
Public string GetBookName ()
{
Return "Silverlight advanced programming";
}
}
}

 

Then we can import all the library implementation classes by importing multiple entries where they need to be used. The detailed code is as follows. The AllowRecomposition = true parameter indicates that the component set is reorganized after a new component is assembled successfully.

[ImportMany (AllowRecomposition = true)]
Public IBookService [] Services {get; set ;}

 

At this time, you can run the program to find that the length of the Services is 3, indicating that the above three books are successfully assembled. Next, we define another implementation (OracleBookService) in a new Silverlight application, and then dynamically load and assemble the application. Here, MEF provides the DeploymentCatalog class, it is used to dynamically load and assemble external Silverlight application packages. You only need to provide the path where the application package is located to automatically download and dynamically assemble the packages. For usage instructions, refer to the following code segment, for more details, see MEF Programming Guide 7: Using Catalog to dynamically load xap and Filtered Catalog.

Var dc = new DeploymentCatalog ("MEFTraining. Recomposition. Parts. xap ");
Dc. DownloadAsync ();
Dc. DownloadCompleted + = (rs, re) =>
{};

 

The following code segment is the fourth implementation part of the book, and will be applied to the demo of the reorganization function.

Namespace MEFTraining. Recomposition. Parts
{
[Export (typeof (IBookService)]
Public partial class OracleBookService: UserControl, IBookService
{
Public OracleBookService ()
{
InitializeComponent ();
}

Public string GetBookName ()
{
Return "Oracle DBA manual";
}
}
}

 

Use DeploymentCatalog to load the application package where the fourth part is located. After debugging, we can see that Services has been restructured and changed from the original three parts to four parts.

This. DService. AddXap ("MEFTraining. Recomposition. Parts. xap", null );

 

During the dynamic download and assembly process, the main program may need to be notified of the successful assembly of a Part accurately, the IPartImportsSatisfiedNotification interface is a notification interface that can accurately listen to the combination of MEF containers when a new part is successfully assembled, once a new plug-in component is imported and loaded to the MEF container, this interface will be automatically notified. There is an interface method internally, detailed code block as follows:

Public void OnImportsSatisfied ()
{

}

 

The following is the complete main program code. For the complete sample code in this article, please download the code attachment.

Public partial class MainPage: UserControl, IPartImportsSatisfiedNotification
{
[Import]
Public IDeploymentService DService {get; set ;}

[ImportMany (AllowRecomposition = true)]
Public IBookService [] Services {get; set ;}

Public MainPage ()
{
InitializeComponent ();
CompositionInitializer. SatisfyImports (this );
}

Private void button#click (object sender, RoutedEventArgs e)
{
This. DService. AddXap ("MEFTraining. Recomposition. Parts. xap", null );
}

Public void OnImportsSatisfied ()
{

}
}

 

References:Recomposition, DeploymentCatalog

MEF Official Website: http://mef.codeplex.com/

Recommendation guide: MEF programming guide 1: Host MEF in an application

MEF Programming Guide 2: Using CompositionInitializer to host MEF in Silverlight

MEF programming guide 3: Basic Application of Composable Parts and Contracts in MEF

MEF programming guide 4: Using MEF to Declare export (Exports) and import (Imports)

MEF programming guide 5: Lazy loading and exporting parts and Metadata)

MEF Program Design Guide 6: DeploymentCatalog in MEF)

MEF Programming Guide 7: Using Catalog to dynamically load xap and Filtered Catalog)

MEF programming guide 8: Part lifecycle (Parts Lifetime) Hosting

 

Description

This document is a learning note and is intended to be shared with people with lofty ideals. You are welcome to repost this article, but mark the original article connection in a prominent position.

Author: Beniao

Article Source: http://beniao.cnblogs.com/or http://www.cnblogs.com/

 

 

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.