DynamicDataSourceView.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 : DataSourceView
  12. {
  13. #region Private fields
  14. DynamicDataSource owner;
  15. IDynamicDataContainer 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 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;
  52. if (inst == null)
  53. throw new InvalidOperationException (
  54. "DynamicDataSource.DataContainerInstance must be set to an instance of IDynamicDataContainer.");
  55. return dataContainer;
  56. }
  57. string typeName = owner.DataContainerTypeName;
  58. if (String.IsNullOrEmpty (typeName))
  59. throw new InvalidOperationException ("DynamicDataSource '" + owner.ID + "' does not specify either data container instance or its type name.");
  60. Type type = Type.GetType (typeName, true);
  61. if (!typeof (IDynamicDataContainer).IsAssignableFrom (type))
  62. throw new InvalidOperationException ("Data container type '" + typeName + "' specified by DynamicDataSource '" + owner.ID + "' does not implement the IDynamicDataContainer interface.");
  63. dataContainer = Activator.CreateInstance (type) as IDynamicDataContainer;
  64. if (dataContainer == null)
  65. throw new InvalidOperationException ("Failed to create instance of data container type '" + typeName + "'.");
  66. return dataContainer;
  67. }
  68. }
  69. #region Constructors
  70. public DynamicDataSourceView (DynamicDataSource owner, string viewName)
  71. : base (owner, viewName)
  72. {
  73. this.owner = owner;
  74. }
  75. #endregion
  76. #region DataSourceView methods
  77. protected override int ExecuteDelete (IDictionary keys, IDictionary oldValues)
  78. {
  79. return DataContainerInstance.Delete (keys, oldValues);
  80. }
  81. protected override int ExecuteInsert (IDictionary values)
  82. {
  83. return DataContainerInstance.Insert (values);
  84. }
  85. protected override int ExecuteUpdate (IDictionary keys, IDictionary values, IDictionary oldValues)
  86. {
  87. return DataContainerInstance.Update (keys, values, oldValues);
  88. }
  89. protected override IEnumerable ExecuteSelect (DataSourceSelectArguments arguments)
  90. {
  91. return DataContainerInstance.Select (arguments, owner.Where, owner.WhereParameters);
  92. }
  93. #endregion
  94. }
  95. }