From ac5c1de26f32023c3f9e98cb10013daa60af9b14 Mon Sep 17 00:00:00 2001 From: Daniel Svensson Date: Thu, 8 Feb 2024 10:34:51 +0100 Subject: [PATCH] improve ListCollectionViewProxy for entitycollection --- .../Framework/EntityCollection.cs | 11 ++++++---- .../Client.Test/Data/EntityCollectionTests.cs | 22 +++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/OpenRiaServices.Client/Framework/EntityCollection.cs b/src/OpenRiaServices.Client/Framework/EntityCollection.cs index ce1a19de..76fb74a9 100644 --- a/src/OpenRiaServices.Client/Framework/EntityCollection.cs +++ b/src/OpenRiaServices.Client/Framework/EntityCollection.cs @@ -620,7 +620,8 @@ private void OnEntityAssociationUpdated(Entity entity) { // Add matching entity to our set. When adding, we use the stronger Filter to // filter out New entities - _ = this.TryAddEntity(typedEntity); + bool added = this.TryAddEntity(typedEntity); + Debug.Assert(added); this.RaiseCollectionChangedNotification(NotifyCollectionChangedAction.Add, typedEntity, this.Entities.Count - 1); } else if (containsEntity && !this._entityPredicate(typedEntity)) @@ -826,7 +827,7 @@ ICollectionView ICollectionViewFactory.CreateView() /// is sufficient for interaction with the ListCollectionView. /// /// The entity type of this proxy - private class ListCollectionViewProxy : IList, IEnumerable, INotifyCollectionChanged, ICollectionChangedListener where T : Entity + internal class ListCollectionViewProxy : IList, IEnumerable, INotifyCollectionChanged, ICollectionChangedListener where T : Entity { private readonly object _syncRoot = new object(); private readonly EntityCollection _source; @@ -856,8 +857,10 @@ public int Add(object value) } this._addedEntities.Add(entity); + int countBefore = this.Source.Count; this.Source.Add(entity); - return this.IndexOf(entity); + + return this.Source.Entities.IndexOf(entity, countBefore); } public void Clear() @@ -868,7 +871,7 @@ public void Clear() public bool Contains(object value) { - return this.IndexOf(value) >= 0; + return this.Source.EntitiesHashSet.Contains(value); } public int IndexOf(object value) diff --git a/src/OpenRiaServices.Client/Test/Client.Test/Data/EntityCollectionTests.cs b/src/OpenRiaServices.Client/Test/Client.Test/Data/EntityCollectionTests.cs index 439d18ce..28a01408 100644 --- a/src/OpenRiaServices.Client/Test/Client.Test/Data/EntityCollectionTests.cs +++ b/src/OpenRiaServices.Client/Test/Client.Test/Data/EntityCollectionTests.cs @@ -49,6 +49,28 @@ public void ICVF_AddNew() "EntitySet should contain the first entity after CommitNew."); } + [TestMethod] + [Description("Tests that ListCollectionViewProxy returns correct index")] + public void ICVF_ListCollectionViewProxy_Add() + { + EntitySet entitySet; + EntityCollection entityCollection = this.CreateEntityCollection(out entitySet); + EntityCollection.ListCollectionViewProxy collection = new(entityCollection); + + for (int i=0; i < 3; ++i) + { + var city = new City() { ZoneID = i }; + + Assert.IsFalse(collection.Contains(city)); + int idx = collection.Add(city); + + Assert.AreEqual(i, idx); + Assert.AreSame(city, collection[idx]); + Assert.IsTrue(collection.Contains(city)); + Assert.IsTrue(entityCollection.Contains(city)); + } + } + [TestMethod] [Description("Tests that calling AddNew on the View adds to the EntityCollection and EntitySet and CancelNew removes both.")] public void ICVF_CancelNew()