Through the first eight articles Program 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 will use the application example in the previous article as a function demonstration scenario. Three different implementation classes under a library interface are defined in the main program, as follows:CodeSection:
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 StringGetbookname ()
{
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)]
PublicIbookservice [] services {Get;Set;}
Public mainpage ()
{< br> initializecomponent ();
compositioninitializer. satisfyimports ( This );
}
private void button#click ( Object sender, routedeventargs e)
{< br> This . dservice. addxap ( " meftraining. recomposition. parts. xap " , null );
}
Public VoidOnimportssatisfied ()
{
}
}
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
Study NotesArticleWe are willing to learn and exchange with those who are interested in the future. 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/