The use of export and import in MEF is essentially the process of instantiating an object. Through the features of MEF, the direct dependency of the object is reduced, this allows the system design to achieve a highly flexible and scalable effect. In specific design and development, some objects do not need to be instantiated during system running or affiliated object initialization, it is instantiated only when it is needed. In terms of the system, this is also a feasible way to improve the system performance, this method can be understood as the delayed initialization of an object, or delayed loading. MEF also provides a sound implementation mechanism for this application scenario. The following describes how to use delayed initialization in MEF.
Namespace MEFTraining. LzayImports
{
Public interface ILogger
{
Void WriteLog (string message );
}
[Export (typeof (ILogger)]
Public class DBLogger: ILogger
{
Public void WriteLog (string message)
{
MessageBox. Show (message );
}
}
}
The log component used in the previous blog is used as an example to Export the [Export] configuration of the object on the specific implementation object of log records. If you use the traditional method to import parts, the following code block is shown:
[Import (typeof (ILogger)]
Public ILogger Logger {get; set ;}
If Lazy loading is required, MEF provides a special method for delayed loading, which uses the Lazy class to complete delayed loading, then, the loaded object is obtained through other attribute values. Use the following code block in detail:
Public partial class MainPage: UserControl
{
// Traditional Loading
[Import (typeof (ILogger)]
Public ILogger Logger {get; set ;}
// Delayed Loading
[Import]
Public Lazy <ILogger> Service;
Public MainPage ()
{
InitializeComponent ();
CompositionInitializer. SatisfyImports (this );
Logger. WriteLog ("log Content ");
Service. Value. WriteLog ("log Content ");
}
}
The detailed information of objects with delayed import can be obtained through debugging and output. The following is the detailed information of the Service and Service. Value output in the Command window.
Service
ThreadSafetyMode = PublicationOnly, IsValueCreated = true, IsValueFaulted = false, Value = {MEFTraining. LzayImports. DBLogger}
IsValueCreated: true
Service. Value
{MEFTraining. LzayImports. DBLogger}
[MEFTraining. LzayImports. DBLogger]: {MEFTraining. LzayImports. DBLogger}
Delayed loading also supports metadata export and import, mainly using the [MetadataAttribute] feature. In actual development, you can customize the metadata structure, here, an empty metadata interface is used to import metadata to the application for demonstration.
Public interface IMetadata
{
}
You can use the metadata feature to declare the exported parts. The following is a simple application.
[MetadataAttribute]
[Export (typeof (Users)]
Public class Users
{
Public string UserName = "zhangsan ";
}
The following code block shows the metadata Import Application:
Public partial class MetadataControl: UserControl
{
[Import (typeof (Users)]
Public Lazy <Users, IMetadata> Users {get; set ;}
Public MetadataControl ()
{
InitializeComponent ();
// Host MEF hosted extended container
CompositionInitializer. SatisfyImports (this );
MessageBox. Show (Users. Value. UserName );
}
}
The debugging output is shown in the following code block:
Users
ThreadSafetyMode = PublicationOnly, IsValueCreated = true, IsValueFaulted = false,
Value
=
{MEFTraining. LzayImports. Users}
Base {System. Lazy <MEFTraining. LzayImports. Users >}: ThreadSafetyMode = PublicationOnly, IsValueCreated = true, IsValueFaulted = false, Value = {MEFTraining. LzayImports. Users}
Metadata: {_ Proxy_MEFTraining.LzayImports.
IMetadata_ 0174a468-9771-4271-a37e-9a4a83eca6bd}
MEF also provides the [ExportMetadata] feature for metadata Import and Export. Using ExportMetadata can basically meet the needs of most metadata export and import support. Modify the preceding example to import and export a custom metadata structure.
Public interface IMetadata
{
String Name {get ;}
}
[ExportMetadata ("Name", "Li Si")]
[Export (typeof (Users)]
Public class Users
{
Public string UserName = "zhangsan ";
}
The sample code above demonstrates how to export the metadata information with the attribute Name "Name" and Its Value "Li Si" through metadata, and defines a structure for carrying the metadata structure, next we can get the metadata by loading the import in a delayed manner.
Public partial class MetadataControl: UserControl
{
[Import (typeof (Users)]
Public Lazy <Users, IMetadata> Users {get; set ;}
Public MetadataControl ()
{
InitializeComponent ();
// Host MEF hosted extended container
CompositionInitializer. SatisfyImports (this );
MessageBox. Show (Users. Value. UserName );
}
}
To allow debugging, we can clearly see that the metadata information in the exported part has been successfully imported to the current object instance attribute during delayed import.
The following is the complete metadata application instance code.
Namespace MEFTraining. LzayImports
{
Public partial class MetadataControl: UserControl
{
[Import (typeof (Users)]
Public Lazy <Users, IMetadata> Users {get; set ;}
Public MetadataControl ()
{
InitializeComponent ();
// Host MEF hosted extended container
CompositionInitializer. SatisfyImports (this );
MessageBox. Show (Users. Value. UserName );
}
}
Public interface IMetadata
{
String Name {get ;}
}
[ExportMetadata ("Name", "Li Si")]
[Export (typeof (Users)]
Public class Users
{
Public string UserName = "zhangsan ";
}
}
In addition, delayed loading also supports weak types of metadata. You can also filter the metadata. Here we will not describe it in detail. If you are interested, you can study it on your own.
Reference:Lazy Imports,Exports and Metadata
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)
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/