MEF Study Notes (5): delayed loading of exported parts

Source: Internet
Author: User
Delayed loading and exporting parts

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. To use it, we need to import and use[System. Lazy <t>]To directly replace[T]. For example, think about the code segment under idea

Public class httpserverhealthmonitor {[import] public imessagesender sender {Get; set ;}

The import code above indicates that it depends on an imessagesender implementation. When MEF provides this dependency, it also needs to create imessagesender to select and implement recursion.
To delay loading of the import, you only need to replace it with lazy <imessagesender>.

[Export] public class httpserverhealthmonitor {[import] public lazy <imessagesender> sender {Get; set ;}

In this example, you can choose to delay the instantiation until you really want to implement the instance. To request an instance, use[Lazy <t>. value]. Attribute.

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.