DynamicDataContainerColumnProvider.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  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 DynamicDataContainerColumnProvider : ColumnProvider
  12. {
  13. DynamicDataColumn column;
  14. bool associationResolved;
  15. public override AssociationProvider Association {
  16. get {
  17. ResolveAssociations ();
  18. return base.Association;
  19. }
  20. protected set {
  21. base.Association = value;
  22. }
  23. }
  24. public DynamicDataContainerColumnProvider (DynamicDataContainerTableProvider owner, DynamicDataColumn column)
  25. : base (owner)
  26. {
  27. if (column == null)
  28. throw new ArgumentNullException ("column");
  29. this.column = column;
  30. Type columnType = column.DataType;
  31. if (columnType == null)
  32. throw new InvalidOperationException ("column.DataType must not be null for column '" + column.Name + "'");
  33. Name = column.Name;
  34. ColumnType = columnType;
  35. Nullable = columnType.IsGenericType && typeof (Nullable<>).IsAssignableFrom (columnType.GetGenericTypeDefinition ());
  36. IsPrimaryKey = column.PrimaryKey;
  37. EntityTypeProperty = GetPropertyInfo (owner.EntityType, Name);
  38. IsCustomProperty = column.CustomProperty;
  39. IsGenerated = column.Generated;
  40. MaxLength = GetMaxLength (EntityTypeProperty);
  41. IsSortable = column.Sortable;
  42. }
  43. public void ResolveAssociations ()
  44. {
  45. if (associationResolved)
  46. return;
  47. associationResolved = true;
  48. string associated = column.AssociatedTo;
  49. if (String.IsNullOrEmpty (associated))
  50. return;
  51. string[] names = associated.Split (new char[] { '.' });
  52. if (names.Length != 2)
  53. throw new ApplicationException ("Only associations of type Table.Column are supported");
  54. string tableName = names[0];
  55. string columnName = names[1];
  56. TableProvider tableProvider = null;
  57. try {
  58. tableProvider = Table.DataModel.Tables.First<TableProvider> ((TableProvider tp) => {
  59. if (tp.Name == tableName)
  60. return true;
  61. return false;
  62. });
  63. } catch {
  64. return;
  65. }
  66. if (tableProvider == null)
  67. return;
  68. ColumnProvider toColumn = null;
  69. try {
  70. toColumn = tableProvider.Columns.First<ColumnProvider> ((ColumnProvider cp) => {
  71. if (cp.Name == columnName)
  72. return true;
  73. return false;
  74. });
  75. } catch {
  76. return;
  77. }
  78. if (toColumn == null)
  79. return;
  80. IsForeignKeyComponent = true;
  81. Association = new DynamicDataAssociationProvider (column.AssociationDirection, this, toColumn);
  82. }
  83. int GetMaxLength (PropertyInfo pi)
  84. {
  85. if (pi == null)
  86. return 0;
  87. object[] attrs = pi.GetCustomAttributes (typeof (DynamicDataStringLengthAttribute), true);
  88. if (attrs == null || attrs.Length == 0)
  89. return 0;
  90. var attr = attrs[0] as DynamicDataStringLengthAttribute;
  91. if (attr == null)
  92. return 0;
  93. return attr.MaxLength;
  94. }
  95. PropertyInfo GetPropertyInfo (Type type, string name)
  96. {
  97. try {
  98. PropertyInfo ret = type.GetProperties (BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy).
  99. First<PropertyInfo> ((pi) => {
  100. if (String.Compare (pi.Name, name, StringComparison.Ordinal) == 0)
  101. return true;
  102. return false;
  103. }
  104. );
  105. return ret;
  106. } catch (InvalidOperationException ex) {
  107. return null;
  108. }
  109. }
  110. }
  111. }