DynamicDataContainerModelProvider.cs 2.4 KB

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