[MEF] No. 03 MEF lazy loading export parts and metadata

Source: Internet
Author: User


I. Overview of the Presentation
This demo describes the lazy loading of MEF and metadata-related content.
In the actual design development, there are some objects that do not need to be instantiated when the system is running or when the subordinate object is initialized.
It only needs to be instantiated when it is used, which can be understood as the delay initialization of an object, or delayed loading. Great for improving the performance of the system!
In addition, you can add metadata to the exported parts to provide more flexible extension methods in your code, or to get more information about them. Even, you can customize the metadata interface, whether it is defined or used, is very convenient.
Related Download (screen recording): http://yunpan.cn/cZCfFBypPSLCh access password 0ce2
Play Online :
Warm tips :
If the screen recording and code can not be downloaded properly, you can leave a message in the station, or send an email to [email protected]

Welcome to be interested in research. NET related technology users add QQ Group: 18507443

Second, delay loading
The settings for lazy loading at the exported part do not change at all, keep the original settings. As shown in the following code:

[CSharp]View PlainCopy 
    1. [Export ("oracal", typeof (ILogger))]
    2. Public class Oracallogger:ilogger
    3. {
    4. public void Writelog (string message)
    5. {
    6. Console.WriteLine ("oracal Logger = {0}", message);
    7. }
    8. }

When importing, you need to use the. NET provides the lazy class to complete. The code is as follows:

[CSharp]View PlainCopy 
    1. [Import ("oracal", typeof (ILogger))]
    2. Public lazy<ilogger> Oracallogger { get;  set;}

Deferred loading is automatically provided by objects in the lazy package. The way to access the object is also slightly changed, of course, not directly through the property (Oracallogger in the above example) to access, but need to pass lazy<t>. value to access, which contains the object even if the delay is loaded.
In addition Lazy<t>. The IsValueCreated property provides the ability to determine whether an object is loaded, or true to indicate that the object is loaded, otherwise it is not loaded.

Third, meta-data
With regard to the use of MEF data, you first need to add the ExportMetaData attribute to the exported part to set the metadata to be added. As shown in the following code:

[CSharp]View PlainCopy 
  1. [ExportMetaData ("Name", "Xml")]
  2. [ExportMetaData ("Description", "using XML to log Logs")]
  3. [Export ("Xml", typeof (ILogger))]
  4. Public class Xmllogger:ilogger
  5. {
  6. public void Writelog (string message)
  7. {
  8. Console.WriteLine ("Xml Logger + = {0}", message);
  9. }
  10. }

As shown in the code above, first the ExportMetaData attribute can be repeatedly added to multiple parts, in addition, two parameters of the attribute are very similar to the key-value pairs, the first parameter is the name of the metadata, the second parameter is the actual content of the metadata.
At this point, the modifications needed to export the part are complete. Next, the changes are imported. As shown in the following code:

[CSharp]View PlainCopy  
    1. [Import ("Xml", typeof (ILogger))]
    2. Public Lazy<ilogger, imetadata> xmllogger { get;  set;}

The lazy class is still used, as shown in the code above. Where the second generic parameter represents the interface of the meta-data. The definitions are broadly as follows:

[CSharp]View PlainCopy 
    1. Public Interface Imetadata
    2. {
    3. string Description { get;}
    4. string Name { get;}
    5. }

A careful comparison will reveal that the member names in the interface and the metadata names in the exported parts are so consistent. It is not only the name to be consistent, even the data type must be consistent, otherwise the import will throw an exception.

After the code modification as above, by importing elements (the Xmllogger property in the previous example) there will be a property named metadata, Lazy<t, Tmeta>. Metadata, it provides access to the individual metadata in the exported part.
As shown in the following code:

[CSharp]View PlainCopy  
    1. Console.WriteLine (program. XmlLogger.Metadata.Name);
    2. Console.WriteLine (program. XmlLogger.Metadata.Description);


See the introduction in the demo video for details.

Iv. Related Resources
1. MSDN Official information: http://msdn.microsoft.com/zh-cn/library/dd460648 (v=vs.110). aspx
2, referring to the Microsoft MVP Bēniaǒ article "MEF Programming Guide V: Delay (Lazy) loading export parts (Metadata)," Access to address: http://www.cnblogs.com/beniao/ Archive/2010/07/14/1777593.html

[MEF] No. 03 MEF lazy loading export parts and metadata

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.