MEF framework learning Tour (10) Restructuring

Source: Internet
Author: User

Reassemble is a feature of MEF, which allows parts to be automatically updated when a new matching export occurs in the system. Reassembly is useful in some scenarios. The allowrecomposition = true parameter indicates that the component set is restructured after a new component is assembled successfully.

For example, when a part is downloaded from a remote server. Salesordermanager can be changed so that multiple optional views can be downloaded when it is started. These views are displayed in the view factory. To enable the viewfactory to be recombined, we set the allowrecomposition attribute to true in the importposition attribute of the Views attribute, as shown below:

Code segment

[Export]public class ViewFactory{[ImportMany(AllowRecomposition=true)]IEnumerable<Lazy<IView, IViewMetadata>> Views { get; set; }public IEnumerable<View>GetViews(ViewTypesviewType) 
{return Views.Where(v=>v.Metadata.ViewType.Equals(viewType)).Select(v=>v.Value);}}

 

The views set is immediately replaced with a new set containing a group of updated views.

After the re-assembly is enabled, the application can download other Assembly from the server and add the assembly to the container. You can perform this operation through the MEF directory. MEF provides multiple directories, two of which can be combined. Directorycatalog (you have seen it) is a directory that can be re-combined by calling its refresh method. Another directory that can be combined again is aggregatecatalog, which is the directory. You can use the catalogs set property to add a directory to the directory, which will restart the combination. The last directory I will use is assemblycatalog, which accepts an assembly on which it will then build the directory.Figure2This example shows how to use these directories for dynamic download.

Figure 2UseMEFDirectory for dynamic download

Code segment

void App_Startup(object sender, StartupEventArgs e){var catalog = new AggregateCatalog();catalog.Catalogs.Add(newDirectoryCatalog((@"\.")));var container = new CompositionContainer(catalog);container.Composeparts(this);base.MainWindow = MainWindow;this.DownloadAssemblies(catalog);} private void DownloadAssemblies(AggregateCatalog catalog){//asynchronously downloads assemblies and calls AddAssemblies} private void AddAssemblies(Assembly[] assemblies, AggregateCatalog catalog){var assemblyCatalogs = new AggregateCatalog();foreach(Assembly assembly in assemblies)assemblyCatalogs.Catalogs.Add(new AssemblyCatalog(assembly));catalog.Catalogs.Add(assemblyCatalogs);}

Figure2The container in is created using aggregatecatalog. The container then adds the directorycatalog to it to obtain the local part in the bin folder. The aggregation directory is passed to the downloadassemblies method. This method asynchronously downloads the Assembly and calls addassemblies. This method creates a new aggregatecatalog and adds assemblycatalogs to the directory for each downloaded assembly. Then, addassemblies adds the aggregatecatalog that contains the main aggregated assembly. This method is used to add a new assembly, instead of repeating it (this occurs when the Assembly directory is directly added ).

The set will be updated immediately when the combination is performed. The result varies depending on the set property type. If the attribute type is ienumerable <t>, it is replaced with the new instance. If it is a specific set inherited from list <t> or icollection, MEF calls clear and add for each item in sequence. In either case, it means that thread security must be taken into account when a combination is used. Combination is not only related to addition, but also to deletion. If the directory is removed from the container, these parts are also removed.

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.