DynamicDataSource.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.DynamicData;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. namespace MonoTests.DataSource
  10. {
  11. [PersistChildren (false)]
  12. [ParseChildren (true)]
  13. public class DynamicDataSource : DataSourceControl, IDynamicDataSource
  14. {
  15. const string DEFAULT_VIEW_NAME = "DefaultView";
  16. static readonly string[] emptyNames = new string[] { "DefaultView" };
  17. DataSourceView defaultView;
  18. ParameterCollection whereCollection;
  19. Type dataContainerType;
  20. public DynamicDataSource ()
  21. : this (null)
  22. {
  23. }
  24. public DynamicDataSource (string dataContainerTypeName)
  25. {
  26. this.DataContainerTypeName = dataContainerTypeName;
  27. }
  28. public DataSourceView DefaultView {
  29. get {
  30. if (defaultView == null)
  31. defaultView = CreateView (DEFAULT_VIEW_NAME);
  32. return defaultView;
  33. }
  34. }
  35. public Type DataContainerType {
  36. get {
  37. if (dataContainerType == null)
  38. dataContainerType = Type.GetType (DataContainerTypeName, true);
  39. return dataContainerType;
  40. }
  41. }
  42. public string DataContainerTypeName {
  43. get;
  44. set;
  45. }
  46. public object DataContainerInstance
  47. {
  48. get;
  49. set;
  50. }
  51. DataSourceView CreateView (string viewName)
  52. {
  53. Type genType = typeof (DynamicDataSourceView<>).GetGenericTypeDefinition ();
  54. return Activator.CreateInstance (genType.MakeGenericType (new Type[] { ContextType }), this, viewName) as DataSourceView;
  55. }
  56. #region DataSourceControl Members
  57. protected override DataSourceView GetView (string viewName)
  58. {
  59. if (String.IsNullOrEmpty (viewName))
  60. return DefaultView;
  61. return CreateView (viewName);
  62. }
  63. #endregion
  64. #region IDynamicDataSource Members
  65. public bool AutoGenerateWhereClause
  66. {
  67. get;
  68. set;
  69. }
  70. public Type ContextType
  71. {
  72. get;
  73. set;
  74. }
  75. public bool EnableDelete
  76. {
  77. get;
  78. set;
  79. }
  80. public bool EnableInsert
  81. {
  82. get;
  83. set;
  84. }
  85. public bool EnableUpdate
  86. {
  87. get;
  88. set;
  89. }
  90. public string EntitySetName
  91. {
  92. get;
  93. set;
  94. }
  95. public event EventHandler<DynamicValidatorEventArgs> Exception;
  96. public string Where
  97. {
  98. get;
  99. set;
  100. }
  101. [PersistenceMode (PersistenceMode.InnerProperty)]
  102. public ParameterCollection WhereParameters
  103. {
  104. get
  105. {
  106. if (whereCollection == null)
  107. whereCollection = new ParameterCollection ();
  108. return whereCollection;
  109. }
  110. }
  111. #endregion
  112. #region IDataSource Members
  113. DataSourceView IDataSource.GetView (string viewName)
  114. {
  115. return GetView (viewName);
  116. }
  117. ICollection IDataSource.GetViewNames ()
  118. {
  119. return emptyNames;
  120. }
  121. #endregion
  122. }
  123. }