AdaptingCollectionTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. // -----------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // -----------------------------------------------------------------------
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.Specialized;
  7. using System.ComponentModel.Composition.Hosting;
  8. using System.ComponentModel.Composition.Primitives;
  9. using System.Linq;
  10. using Microsoft.VisualStudio.TestTools.UnitTesting;
  11. using System.ComponentModel.Composition.Factories;
  12. namespace System.ComponentModel.Composition
  13. {
  14. public class FilteringCollection<T, M> : AdaptingCollection<T, M>
  15. {
  16. public FilteringCollection(Func<Lazy<T, M>, bool> filter)
  17. : base(e => e.Where(filter))
  18. {
  19. }
  20. }
  21. public class OrderingCollection<T, M> : AdaptingCollection<T, M>
  22. {
  23. public OrderingCollection(Func<Lazy<T, M>, object> keySelector)
  24. : this(keySelector, false)
  25. {
  26. }
  27. public OrderingCollection(Func<Lazy<T, M>, object> keySelector, bool descending)
  28. : base(e => descending ? e.OrderByDescending(keySelector) : e.OrderBy(keySelector))
  29. {
  30. }
  31. }
  32. public class AdaptingCollection<T> : AdaptingCollection<T, IDictionary<string, object>>
  33. {
  34. public AdaptingCollection(Func<IEnumerable<Lazy<T, IDictionary<string, object>>>,
  35. IEnumerable<Lazy<T, IDictionary<string, object>>>> adaptor)
  36. : base(adaptor)
  37. {
  38. }
  39. }
  40. public class AdaptingCollection<T, M> : ICollection<Lazy<T, M>>, INotifyCollectionChanged
  41. {
  42. private readonly List<Lazy<T, M>> _allItems = new List<Lazy<T, M>>();
  43. private readonly Func<IEnumerable<Lazy<T, M>>, IEnumerable<Lazy<T, M>>> _adaptor = null;
  44. private List<Lazy<T, M>> _adaptedItems = null;
  45. public AdaptingCollection() : this(null)
  46. {
  47. }
  48. public AdaptingCollection(Func<IEnumerable<Lazy<T, M>>, IEnumerable<Lazy<T, M>>> adaptor)
  49. {
  50. this._adaptor = adaptor;
  51. }
  52. public event NotifyCollectionChangedEventHandler CollectionChanged;
  53. public void ReapplyAdaptor()
  54. {
  55. if (this._adaptedItems != null)
  56. {
  57. this._adaptedItems = null;
  58. this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
  59. }
  60. }
  61. protected virtual IEnumerable<Lazy<T, M>> Adapt(IEnumerable<Lazy<T, M>> collection)
  62. {
  63. if (this._adaptor != null)
  64. {
  65. return this._adaptor.Invoke(collection);
  66. }
  67. return collection;
  68. }
  69. protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
  70. {
  71. NotifyCollectionChangedEventHandler collectionChanged = this.CollectionChanged;
  72. if (collectionChanged != null)
  73. {
  74. collectionChanged.Invoke(this, e);
  75. }
  76. }
  77. private List<Lazy<T, M>> AdaptedItems
  78. {
  79. get
  80. {
  81. if (this._adaptedItems == null)
  82. {
  83. this._adaptedItems = Adapt(this._allItems).ToList();
  84. }
  85. return this._adaptedItems;
  86. }
  87. }
  88. #region ICollection Implementation
  89. // Accessors work directly against adapted collection
  90. public bool Contains(Lazy<T, M> item)
  91. {
  92. return this.AdaptedItems.Contains(item);
  93. }
  94. public void CopyTo(Lazy<T, M>[] array, int arrayIndex)
  95. {
  96. this.AdaptedItems.CopyTo(array, arrayIndex);
  97. }
  98. public int Count
  99. {
  100. get { return this.AdaptedItems.Count; }
  101. }
  102. public bool IsReadOnly
  103. {
  104. get { return false; }
  105. }
  106. public IEnumerator<Lazy<T, M>> GetEnumerator()
  107. {
  108. return this.AdaptedItems.GetEnumerator();
  109. }
  110. Collections.IEnumerator Collections.IEnumerable.GetEnumerator()
  111. {
  112. return this.GetEnumerator();
  113. }
  114. // Mutation methods work against complete collection
  115. // and then force a reset of the adapted collection
  116. public void Add(Lazy<T, M> item)
  117. {
  118. this._allItems.Add(item);
  119. ReapplyAdaptor();
  120. }
  121. public void Clear()
  122. {
  123. this._allItems.Clear();
  124. ReapplyAdaptor();
  125. }
  126. public bool Remove(Lazy<T, M> item)
  127. {
  128. bool removed = this._allItems.Remove(item);
  129. ReapplyAdaptor();
  130. return removed;
  131. }
  132. #endregion
  133. }
  134. [TestClass]
  135. public class AdaptingCollectionTests
  136. {
  137. public interface IContract { }
  138. public interface INetworkAwareMetadata
  139. {
  140. [DefaultValue(false)]
  141. bool RequiresOnline { get; }
  142. }
  143. [Export(typeof(IContract))]
  144. [ExportMetadata("RequiresOnline", true)]
  145. public class NetworkExport : IContract { }
  146. [Export(typeof(IContract))]
  147. public class NonNetworkExport : IContract { }
  148. public class FilterExports
  149. {
  150. public FilterExports()
  151. {
  152. this.OnlineOnly = new AdaptingCollection<IContract, INetworkAwareMetadata>(e =>
  153. e.Where(p => p.Metadata.RequiresOnline));
  154. this.OnlineOnly2 = new FilteringCollection<IContract, INetworkAwareMetadata>(p => p.Metadata.RequiresOnline);
  155. }
  156. [ImportMany]
  157. public AdaptingCollection<IContract, INetworkAwareMetadata> OnlineOnly { get; set; }
  158. [ImportMany]
  159. public FilteringCollection<IContract, INetworkAwareMetadata> OnlineOnly2 { get; set; }
  160. }
  161. [TestMethod]
  162. public void TestFilteringImports()
  163. {
  164. var container = ContainerFactory.CreateWithAttributedCatalog(typeof(NetworkExport), typeof(NonNetworkExport));
  165. var filterExports = new FilterExports();
  166. container.ComposeParts(filterExports);
  167. Assert.AreEqual(1, filterExports.OnlineOnly.Count);
  168. Assert.AreEqual(1, filterExports.OnlineOnly2.Count);
  169. }
  170. public interface IOrderMetadata
  171. {
  172. [DefaultValue(Int32.MaxValue)]
  173. int Order { get; }
  174. }
  175. [Export(typeof(IContract))]
  176. [ExportMetadata("Order", 2)]
  177. public class BExport : IContract { }
  178. [Export(typeof(IContract))]
  179. [ExportMetadata("Order", 1)]
  180. public class AExport : IContract { }
  181. [Export(typeof(IContract))]
  182. public class CExport : IContract { }
  183. public class OrderExportsByMetadata
  184. {
  185. public OrderExportsByMetadata()
  186. {
  187. this.OrderedItems = new AdaptingCollection<IContract, IOrderMetadata>(e =>
  188. e.OrderBy(p => p.Metadata.Order));
  189. this.OrderedItems2 = new OrderingCollection<IContract, IOrderMetadata>(p => p.Metadata.Order);
  190. }
  191. [ImportMany]
  192. public AdaptingCollection<IContract, IOrderMetadata> OrderedItems { get; set; }
  193. [ImportMany]
  194. public OrderingCollection<IContract, IOrderMetadata> OrderedItems2 { get; set; }
  195. }
  196. [TestMethod]
  197. public void TestOrderingImportsByMetadata()
  198. {
  199. var container = ContainerFactory.CreateWithAttributedCatalog(typeof(BExport), typeof(AExport), typeof(CExport));
  200. var orderExports = new OrderExportsByMetadata();
  201. container.ComposeParts(orderExports);
  202. Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(0).Value, typeof(AExport));
  203. Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(1).Value, typeof(BExport));
  204. Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(2).Value, typeof(CExport));
  205. Assert.IsInstanceOfType(orderExports.OrderedItems2.ElementAt(0).Value, typeof(AExport));
  206. Assert.IsInstanceOfType(orderExports.OrderedItems2.ElementAt(1).Value, typeof(BExport));
  207. Assert.IsInstanceOfType(orderExports.OrderedItems2.ElementAt(2).Value, typeof(CExport));
  208. }
  209. public class OrderExportsByName
  210. {
  211. public OrderExportsByName(bool descending)
  212. {
  213. if (descending)
  214. {
  215. this.OrderedItems = new AdaptingCollection<IContract>(e =>
  216. e.OrderByDescending(p => p.Value.GetType().FullName));
  217. }
  218. else
  219. {
  220. this.OrderedItems = new AdaptingCollection<IContract>(e =>
  221. e.OrderBy(p => p.Value.GetType().FullName));
  222. }
  223. }
  224. [ImportMany]
  225. public AdaptingCollection<IContract> OrderedItems { get; set; }
  226. }
  227. [TestMethod]
  228. public void TestOrderingImportsByTypeName()
  229. {
  230. var container = ContainerFactory.CreateWithAttributedCatalog(typeof(BExport), typeof(AExport), typeof(CExport));
  231. var orderExports = new OrderExportsByName(false);
  232. container.ComposeParts(orderExports);
  233. Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(0).Value, typeof(AExport));
  234. Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(1).Value, typeof(BExport));
  235. Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(2).Value, typeof(CExport));
  236. orderExports = new OrderExportsByName(true);
  237. container.ComposeParts(orderExports);
  238. Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(0).Value, typeof(CExport));
  239. Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(1).Value, typeof(BExport));
  240. Assert.IsInstanceOfType(orderExports.OrderedItems.ElementAt(2).Value, typeof(AExport));
  241. }
  242. public interface IDynamicFilteredMetadata
  243. {
  244. bool Dynamic { get; }
  245. }
  246. [Export(typeof(IContract))]
  247. [ExportMetadata("Dynamic", true)]
  248. public class Dynamic1 : IContract { }
  249. [Export(typeof(IContract))]
  250. [ExportMetadata("Dynamic", true)]
  251. public class Dynamic2 : IContract { }
  252. [Export(typeof(IContract))]
  253. [ExportMetadata("Dynamic", false)]
  254. public class NonDynamic1 : IContract { }
  255. public class DynamicFilteredCollection<T, M> : AdaptingCollection<T, M> where M : IDynamicFilteredMetadata
  256. {
  257. public DynamicFilteredCollection()
  258. {
  259. }
  260. private bool _includeDynamic = false;
  261. public bool IncludeDynamic
  262. {
  263. get { return this._includeDynamic; }
  264. set
  265. {
  266. if (this._includeDynamic != value)
  267. {
  268. this.ReapplyAdaptor();
  269. }
  270. this._includeDynamic = value;
  271. }
  272. }
  273. protected override IEnumerable<Lazy<T, M>> Adapt(IEnumerable<Lazy<T, M>> collection)
  274. {
  275. return collection.Where(p => !p.Metadata.Dynamic || IncludeDynamic);
  276. }
  277. }
  278. public class DynamicExports
  279. {
  280. [ImportMany]
  281. public DynamicFilteredCollection<IContract, IDynamicFilteredMetadata> DynamicCollection { get; set; }
  282. }
  283. [TestMethod]
  284. public void TestDyamicallyFilteringImports()
  285. {
  286. var container = ContainerFactory.CreateWithAttributedCatalog(typeof(Dynamic1), typeof(Dynamic2), typeof(NonDynamic1));
  287. var dynamicExports = new DynamicExports();
  288. container.ComposeParts(dynamicExports);
  289. Assert.AreEqual(1, dynamicExports.DynamicCollection.Count);
  290. dynamicExports.DynamicCollection.IncludeDynamic = true;
  291. Assert.AreEqual(3, dynamicExports.DynamicCollection.Count);
  292. }
  293. public class DynamicExportsNoSubType
  294. {
  295. public DynamicExportsNoSubType()
  296. {
  297. this.DynamicCollection = new AdaptingCollection<IContract, IDynamicFilteredMetadata>(e =>
  298. e.Where(p => !p.Metadata.Dynamic || this.IncludeDynamic));
  299. }
  300. private bool _includeDynamic = false;
  301. public bool IncludeDynamic
  302. {
  303. get { return this._includeDynamic; }
  304. set
  305. {
  306. if (this._includeDynamic != value)
  307. {
  308. this.DynamicCollection.ReapplyAdaptor();
  309. }
  310. this._includeDynamic = value;
  311. }
  312. }
  313. [ImportMany]
  314. public AdaptingCollection<IContract, IDynamicFilteredMetadata> DynamicCollection { get; set; }
  315. }
  316. [TestMethod]
  317. public void TestDyamicallyFilteringNoSubTypeImports()
  318. {
  319. var container = ContainerFactory.CreateWithAttributedCatalog(typeof(Dynamic1), typeof(Dynamic2), typeof(NonDynamic1));
  320. var dynamicExports = new DynamicExportsNoSubType();
  321. container.ComposeParts(dynamicExports);
  322. Assert.AreEqual(1, dynamicExports.DynamicCollection.Count);
  323. dynamicExports.IncludeDynamic = true;
  324. Assert.AreEqual(3, dynamicExports.DynamicCollection.Count);
  325. }
  326. }
  327. }