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.