EntitySetDataBindingList.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Diagnostics.CodeAnalysis;
  6. namespace System.Data.Linq {
  7. internal class EntitySetBindingList<TEntity> : SortableBindingList<TEntity>
  8. where TEntity : class {
  9. private EntitySet<TEntity> data;
  10. private TEntity addNewInstance;
  11. private TEntity cancelNewInstance;
  12. private bool addingNewInstance;
  13. internal EntitySetBindingList(IList<TEntity> sequence, EntitySet<TEntity> data)
  14. : base(sequence) {
  15. if (sequence == null) {
  16. throw Error.ArgumentNull("sequence");
  17. }
  18. if (data == null) {
  19. throw Error.ArgumentNull("data");
  20. }
  21. this.data = data;
  22. }
  23. [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification="Unknown reason.")]
  24. private void ThrowEntitySetErrorsIfTypeInappropriate() {
  25. Type type = typeof(TEntity);
  26. if (type.IsAbstract) {
  27. throw Error.EntitySetDataBindingWithAbstractBaseClass(type.Name);
  28. }
  29. if (type.GetConstructor(System.Type.EmptyTypes) == null) {
  30. throw Error.EntitySetDataBindingWithNonPublicDefaultConstructor(type.Name);
  31. }
  32. }
  33. protected override object AddNewCore() {
  34. ThrowEntitySetErrorsIfTypeInappropriate();
  35. addingNewInstance = true;
  36. addNewInstance = (TEntity)base.AddNewCore();
  37. return addNewInstance;
  38. }
  39. protected override void InsertItem(int index, TEntity item) {
  40. base.InsertItem(index, item);
  41. if (!addingNewInstance && index >= 0 && index <= Count) {
  42. this.data.Insert(index, item);
  43. }
  44. }
  45. protected override void RemoveItem(int index) {
  46. if (index >= 0 && index < Count && this[index] == cancelNewInstance) {
  47. cancelNewInstance = null;
  48. }
  49. else {
  50. this.data.Remove(this[index]);
  51. }
  52. base.RemoveItem(index);
  53. }
  54. protected override void SetItem(int index, TEntity item) {
  55. TEntity removedItem = this[index];
  56. base.SetItem(index, item);
  57. if (index >= 0 && index < Count) {
  58. //Check to see if the user is trying to set an item that is currently being added via AddNew
  59. //If so then the list should not continue the AddNew; but instead add the item
  60. //that is being passed in.
  61. if (removedItem == addNewInstance) {
  62. addNewInstance = null;
  63. addingNewInstance = false;
  64. }
  65. else {
  66. this.data.Remove(removedItem);
  67. }
  68. this.data.Insert(index,item);
  69. }
  70. }
  71. protected override void ClearItems() {
  72. this.data.Clear();
  73. base.ClearItems();
  74. }
  75. public override void EndNew(int itemIndex) {
  76. if (itemIndex >= 0 && itemIndex < Count && this[itemIndex] == addNewInstance) {
  77. this.data.Add(addNewInstance);
  78. addNewInstance = null;
  79. addingNewInstance = false;
  80. }
  81. base.EndNew(itemIndex);
  82. }
  83. public override void CancelNew(int itemIndex) {
  84. if (itemIndex >= 0 && itemIndex < Count && this[itemIndex] == addNewInstance) {
  85. cancelNewInstance = addNewInstance;
  86. addNewInstance = null;
  87. addingNewInstance = false;
  88. }
  89. base.CancelNew(itemIndex);
  90. }
  91. }
  92. }