DynamicDataSourceView.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Web;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. namespace MonoTests.DataSource
  10. {
  11. public class DynamicDataSourceView<T> : DataSourceView
  12. {
  13. #region Private fields
  14. DynamicDataSource owner;
  15. IDynamicDataContainer<T> dataContainer;
  16. #endregion
  17. #region Can* overrides
  18. public override bool CanDelete
  19. {
  20. get { return true; }
  21. }
  22. public override bool CanInsert
  23. {
  24. get { return true; }
  25. }
  26. public override bool CanPage
  27. {
  28. get { return true; }
  29. }
  30. public override bool CanRetrieveTotalRowCount
  31. {
  32. get { return true; }
  33. }
  34. public override bool CanSort
  35. {
  36. get { return true; }
  37. }
  38. public override bool CanUpdate
  39. {
  40. get { return true; }
  41. }
  42. #endregion
  43. public IDynamicDataContainer<T> DataContainerInstance
  44. {
  45. get
  46. {
  47. if (dataContainer != null)
  48. return dataContainer;
  49. object inst = owner.DataContainerInstance;
  50. if (inst != null) {
  51. dataContainer = inst as IDynamicDataContainer<T>;
  52. if (dataContainer == null)
  53. throw new InvalidOperationException (
  54. "DynamicDataSource.DataContainerInstance must be set to an instance of '" + typeof (IDynamicDataContainer <T>) + "'.");
  55. if (String.IsNullOrEmpty (dataContainer.TableName))
  56. dataContainer.TableName = owner.EntitySetName;
  57. return dataContainer;
  58. }
  59. string typeName = owner.DataContainerTypeName;
  60. if (String.IsNullOrEmpty (typeName))
  61. throw new InvalidOperationException ("DynamicDataSource '" + owner.ID + "' does not specify either data container instance or its type name.");
  62. Type type = Type.GetType (typeName, true);
  63. if (!typeof (IDynamicDataContainer<T>).IsAssignableFrom (type))
  64. throw new InvalidOperationException ("Data container type '" + typeName + "' specified by DynamicDataSource '" + owner.ID + "' does not implement the IDynamicDataContainer interface.");
  65. dataContainer = Activator.CreateInstance (type, owner.EntitySetName) as IDynamicDataContainer<T>;
  66. if (dataContainer == null)
  67. throw new InvalidOperationException ("Failed to create instance of data container type '" + typeName + "'.");
  68. return dataContainer;
  69. }
  70. }
  71. #region Constructors
  72. public DynamicDataSourceView (DynamicDataSource owner, string viewName)
  73. : base (owner, viewName)
  74. {
  75. this.owner = owner;
  76. }
  77. #endregion
  78. #region DataSourceView methods
  79. protected override int ExecuteDelete (IDictionary keys, IDictionary oldValues)
  80. {
  81. return DataContainerInstance.Delete (keys, oldValues);
  82. }
  83. protected override int ExecuteInsert (IDictionary values)
  84. {
  85. return DataContainerInstance.Insert (values);
  86. }
  87. protected override int ExecuteUpdate (IDictionary keys, IDictionary values, IDictionary oldValues)
  88. {
  89. return DataContainerInstance.Update (keys, values, oldValues);
  90. }
  91. protected override IEnumerable ExecuteSelect (DataSourceSelectArguments arguments)
  92. {
  93. return DataContainerInstance.Select (arguments, owner.Where, owner.WhereParameters);
  94. }
  95. #endregion
  96. }
  97. }