[MEF] the NO. 05 MEF Catalog (catalog) filter

Source: Internet
Author: User

I. Overview of the Presentation
This example shows how to implement a custom catalog class that can filter the exported parts using the extension mechanism of the MEF-provided catalog (catalog). Mainly by inheriting the Composablepartcatalog base class, and implementing the interface inotifycomposablepartcatalogchanged to complete.
Related Download (screen recording): Http://yunpan.cn/cVkvuUNfuDtTX access password 567d
Warm tip: If the screen recording and code can not be downloaded, 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, custom Parts Catalog category Catalog

In MEF, you can define your own catalog classes in addition to the directory classes that you provide to inject aggregatecatalog, Assemblycatalog, Directorycatalog, and so on.
Custom directory classes need to inherit from the Composablepartcatalog class and implement Interface inotifycomposablepartcatalogchanged. The code looks like the following:

[CSharp]View PlainCopy 
  1. Public class Filteredcatalog:composablepartcatalog, inotifycomposablepartcatalogchanged
  2. {
  3. #region Private Fields
  4. private readonly composablepartcatalog M_composablepartcatalog;
  5. private readonly inotifycomposablepartcatalogchanged m_notifycomposablepartcatalogchanged;
  6. private readonly iqueryable<composablepartdefinition> m_parts;
  7. #endregion
  8. #region Constructors
  9. // <summary>
  10. /// default constructor.
  11. // </summary>
  12. /// <param name= "composablepartcatalog" > directory catalog containing all exported parts. </param>
  13. /// <param name= "expression" > Filter criteria expression. </param>
  14. Public Filteredcatalog (Composablepartcatalog composablepartcatalog, expression<func< Composablepartdefinition, bool>> expression)
  15. {
  16. M_composablepartcatalog = Composablepartcatalog;
  17. m_notifycomposablepartcatalogchanged = Composablepartcatalog as inotifycomposablepartcatalogchanged;
  18. M_parts = composablePartCatalog.Parts.Where (expression);
  19. }
  20. #endregion
  21. #region inotifycomposablepartcatalogchanged
  22. // <summary>
  23. /// component catalog The event that was triggered after the catalogs have changed.
  24. // </summary>
  25. public Event eventhandler<composablepartcatalogchangeeventargs> Changed
  26. {
  27. Add
  28. {
  29. if (m_notifycomposablepartcatalogchanged! = null)
  30. {
  31. M_notifycomposablepartcatalogchanged.changed + = value;
  32. }
  33. }
  34. Remove
  35. {
  36. if (m_notifycomposablepartcatalogchanged! = null)
  37. {
  38. M_notifycomposablepartcatalogchanged.changed-= value;
  39. }
  40. }
  41. }
  42. // <summary>
  43. /// The event that is triggered when the catalog catalog is changing.
  44. // </summary>
  45. public event eventhandler<composablepartcatalogchangeeventargs> changing
  46. {
  47. Add
  48. {
  49. if (m_notifycomposablepartcatalogchanged! = null)
  50. {
  51. M_notifycomposablepartcatalogchanged.changing + = value;
  52. }
  53. }
  54. Remove
  55. {
  56. if (m_notifycomposablepartcatalogchanged! = null)
  57. {
  58. M_notifycomposablepartcatalogchanged.changing-= value;
  59. }
  60. }
  61. }
  62. #endregion
  63. #region Composablepartcatalog
  64. // <summary>
  65. /// Gets the part definition contained in the catalog.  After the expression in the constructor has been filtered, it is already part of the exported part in the Directory Catalog object.
  66. // </summary>
  67. public override iqueryable<composablepartdefinition> Parts
  68. {
  69. get { return m_parts;}
  70. }
  71. #endregion
  72. }

The above code contains the following points, as a general rule:
1, the constructor passed the base directory catalog object, this directory object may contain a lot of export parts, we want to implement the Directory filter class Filteredcatalog is based on this directory to filter, it is a complete, and Filteredcatalog is a subset of it. Another parameter is the filter expression, where the filter is written by the caller, and the internal filtering method is actually the where () approach provided by LINQ.
2, for the implementation of interface inotifycomposablepartcatalogchanged, is actually associated with the event of the base directory catalog object, that is, when the underlying directory object changes, The directory filter class Filteredcatalog will also be notified accordingly.
3. Overrides the Parts collection property of the base class Composablepartcatalog, which returns the part definition that is contained in the directory, and the part definition that needs to be exposed in the catalog is returned through the collection. Therefore, the above code returns the filtered part definition through this property.

The filter class is defined and the next step is how to use it.

Iii. using the Custom catalog class catalog
As shown in the following code:

[CSharp]View PlainCopy 
  1. Get the parts you want.
  2. Directorycatalog Catalog = new Directorycatalog ("controls");
  3. Compositioncontainer container = new Compositioncontainer (catalog);
  4. Filter Catalog to create sub-assembly containers.
  5. Filteredcatalog Filteredcatalog = new filteredcatalog (catalog,
  6. O=>o.metadata.containskey ("UC") && o.metadata["UC"].  ToString () = = "BB");
  7. Compositioncontainer Filteredcontainer = new Compositioncontainer (Filteredcatalog, container);
  8. UserControl UserControl = filteredcontainer.getexportedvalue<usercontrol> ();
  9. This. Maincontentcontrol.content = UserControl;

First, through the Directorycatalog class, get all the part definitions in the Controls subfolder in the application root and generate the top-level combo container container (type Compositioncontainer).
Then use the custom Catalog filter class Filteredcatalog to filter the part definitions in the Directorycatalog directory and generate the sub-combo container filteredcontainer (type Compositioncontainer).
Finally, the export part of the specified protocol type is obtained by combining the getexportedvalue<t> () method of the container. It is necessary to note that an exception is thrown if there are no exported parts of the corresponding protocol type in the composition container.

Complete sample Code and screen recording files can be obtained from the following address.

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 VII: Use directory (catalog) dynamic loading XAP and directory filtering (Filtered catalog)", Access address: http://www.cnblogs.com/beniao/ Archive/2010/07/26/1782622.html

[MEF] the NO. 05 MEF Catalog (catalog) filter

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.