Delay loading of MEF notes and delay of mef notes

Source: Internet
Author: User

Delay loading of MEF notes and delay of mef notes

Reference: Implementation of delayed loading parts in MEF Author: TianFang

Using System; using System. collections. generic; using System. componentModel. composition; using System. componentModel. composition. hosting; namespace delayed loading {interface ILogger {void Log (string message);} [Export (typeof (ILogger)] class ConsoleLogger: ILogger {public void Log (string message) {Console. writeLine ("logger 1" + message) ;}} class Host {[Import] // Lazy <ILogger> _ logger; // non-delayed loading // ILogger _ logger; public Host () {var catalog = new AssemblyCatalog (this. getType (). assembly); var container = new CompositionContainer (catalog); // if it is not delayed loading, the ConsoleLogger object container will be created here. composeParts (this); // non-delayed loading // _ logger. log ("logworld"); // delay loading _ logger. value. log ("hello world ");}}}View Code

 

When a component is not immediately needed, or the memory overhead is high. Affects the speed of program loading. For example, when the program starts. In this case, we can use delayed loading, that is, the part will be loaded only when the program is used. We can use Lazy <T> to mark the import type. In this way, latency loading is implemented.

NOTE: If Lazy <T> is used to mark the object type, you need to call the instance method _ logger. Value. Log ("hello world") through the Value attribute of the instance ");

If the load is not delayed

Using System; using System. collections. generic; using System. componentModel. composition; using System. componentModel. composition. hosting; using System. linq; namespace delayed loading {interface ILogger {void Log (string message);} [Export (typeof (ILogger)] class ConsoleLogger: ILogger {public void Log (string message) {Console. writeLine ("ConsoleLogger" + message);} [Export (typeof (ILogger)] class DbLogger: ILogger {public void Log (string message) {Console. writeLine ("DbLogger" + message);} class Host {// non-delayed loading // [importtasks] // ILogger [] _ logger = null; // delay loading [importger] Lazy <ILogger> [] _ Lazylogger = null; public Host () {var catalog = new AssemblyCatalog (this. getType (). assembly); var container = new CompositionContainer (catiner); // non-delayed loading all objects will be created at this time. composeParts (this); // non-delayed loading // _ logger. firstOrDefault (I => I is DbLogger ). log ("hello world"); // delayed loading. The object is created only when it is called. // However, because there is a traversal, therefore, when an object is called, the object _ Lazylogger will be created. firstOrDefault (I => I. value is DbLogger ). value. log ("DbLogger ");}}}View Code

 

In this case, you can mark the import with importvolumes and use Lazy <T> to package our import type. But there is a problem at this time, that is, if we traverse multiple objects through the type to find available import, all objects will be created. At this time, we can use metadata to determine whether to enable the required import.

Use metadata to match the required imported using System; using System. collections. generic; using System. componentModel. composition; using System. componentModel. composition. hosting; using System. linq; namespace delayed loading {interface ILogger {void Log (string message);} // mark the export service by marking metadata [ExportMetadata ("Name", "Console Logger")] [Export (typeof (ILogger)] class ConsoleLogger: ILogger {public void Log (string message) {Console. writeLine ("ConsoleLogger" + message) ;}// mark the export service by marking metadata [ExportMetadata ("Name", "DbLogger")] [Export (typeof (ILogger)] class DbLogger: ILogger {public void Log (string message) {Console. writeLine ("DbLogger" + message) ;}} public interface ILoggerData {string Name {get ;}} class Host {// delays loading [importtasks] Lazy <ILogger, ILoggerData> [] _ Lazylogger = null; public Host () {var catalog = new AssemblyCatalog (this. getType (). assembly); var container = new CompositionContainer (catiner); // non-delayed loading all objects will be created at this time. composeParts (this); // delayed loading. The object is created only when it is called. // However, because there is a traversal, therefore, when an object is called, the object // _ Lazylogger will be created. firstOrDefault (I => I. value is DbLogger ). value. log ("DbLogger"); _ Lazylogger. firstOrDefault (I => I. metadata. name = "DbLogger "). value. log ("DbLogger ");}}}View Code

Metadata can match the export service we need. However, it is troublesome to simply mark [ExportMetadata ("Name", "DbLogger")], and the code is not neat when there are too many property notes. We can encapsulate a metadata feature.

Using System; using System. collections. generic; using System. componentModel. composition; using System. componentModel. composition. hosting; using System. linq; namespace delayed loading {interface ILogger {void Log (string message);} // mark the export service by marking metadata [LoggerData ("Console Logger")] [Export (typeof (ILogger)] class ConsoleLogger: ILogger {public void Log (string message) {Console. writeLine ("ConsoleLogger" + message) ;}// mark the Export service by marking metadata [LoggerData ("DbLogger")] [Export (typeof (ILogger)] class DbLogger: ILogger {public void Log (string message) {Console. writeLine ("DbLogger" + message) ;}} public interface ILoggerData {string Name {get;} [MetadataAttribute] [AttributeUsage (AttributeTargets. class, Inherited = false, AllowMultiple = false)] class LoggerDataAttribute: Attribute, ILoggerData {public string Name {get; private set;} public LoggerDataAttribute (string name) {this. name = name ;}} class Host {// delay loading [ImportMany] Lazy <ILogger, ILoggerData> [] _ Lazylogger = null; public Host () {var catalog = new AssemblyCatalog (this. getType (). assembly); var container = new CompositionContainer (catiner); // non-delayed loading all objects will be created at this time. composeParts (this); // delayed loading. The object is created only when it is called. // However, because there is a traversal, therefore, when an object is called, the object // _ Lazylogger will be created. firstOrDefault (I => I. value is DbLogger ). value. log ("DbLogger"); _ Lazylogger. firstOrDefault (I => I. metadata. name = "DbLogger "). value. log ("DbLogger ");}}}

 

Address: http://www.cnblogs.com/santian/p/4357324.html

Blog: http://www.cnblogs.com/santian/

For reprinting, please use hyperlinks to indicate the original source of the article.

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.