DynamicDataContainerModelProvider.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.DynamicData;
  7. using System.Web.DynamicData.ModelProviders;
  8. using MonoTests.DataSource;
  9. namespace MonoTests.ModelProviders
  10. {
  11. public class DynamicDataContainerModelProvider : DataModelProvider
  12. {
  13. IDynamicDataContainer container;
  14. Type containerType;
  15. ReadOnlyCollection<TableProvider> tables;
  16. IDynamicDataContainer Container
  17. {
  18. get
  19. {
  20. if (container != null)
  21. return container;
  22. container = Activator.CreateInstance (containerType) as IDynamicDataContainer;
  23. if (container == null)
  24. throw new InvalidOperationException ("Failed to create an instance of container type '" + ContextType + "'.");
  25. return container;
  26. }
  27. }
  28. public override Type ContextType
  29. {
  30. get
  31. {
  32. return Container.ContainedType;
  33. }
  34. protected set
  35. {
  36. throw new InvalidOperationException ("Setting the context type on this provider is not supported.");
  37. }
  38. }
  39. public DynamicDataContainerModelProvider (Type containerType)
  40. {
  41. if (containerType == null)
  42. throw new ArgumentNullException ("contextType");
  43. if (!typeof (IDynamicDataContainer).IsAssignableFrom (containerType))
  44. throw new ArgumentException ("Container type must implement the IDynamicDataContainer interface.", "contextType");
  45. this.containerType = containerType;
  46. }
  47. public DynamicDataContainerModelProvider (IDynamicDataContainer container)
  48. {
  49. if (container == null)
  50. throw new ArgumentNullException ("container");
  51. this.container = container;
  52. }
  53. public override object CreateContext ()
  54. {
  55. return Activator.CreateInstance (ContextType);
  56. }
  57. public override ReadOnlyCollection<TableProvider> Tables
  58. {
  59. get
  60. {
  61. if (tables != null)
  62. return tables;
  63. tables = LoadTables ();
  64. return tables;
  65. }
  66. }
  67. void ResolveAssociations ()
  68. {
  69. foreach (var t in Tables) {
  70. var table = t as DynamicDataContainerTableProvider;
  71. if (t == null)
  72. continue;
  73. table.ResolveAssociations ();
  74. }
  75. }
  76. ReadOnlyCollection<TableProvider> LoadTables ()
  77. {
  78. List<DynamicDataTable> containerTables = Container.GetTables ();
  79. if (containerTables == null || containerTables.Count == 0)
  80. return new ReadOnlyCollection<TableProvider> (new List<TableProvider> ());
  81. var tables = new List<TableProvider> ();
  82. foreach (var table in containerTables)
  83. tables.Add (new DynamicDataContainerTableProvider (this, table));
  84. return new ReadOnlyCollection<TableProvider> (tables);
  85. }
  86. }
  87. }