Skip to content

Commit

Permalink
improve ListCollectionViewProxy for entitycollection
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Svensson committed Feb 8, 2024
1 parent 60766a5 commit ac5c1de
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/OpenRiaServices.Client/Framework/EntityCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -826,7 +827,7 @@ ICollectionView ICollectionViewFactory.CreateView()
/// is sufficient for interaction with the ListCollectionView.
/// </remarks>
/// <typeparam name="T">The entity type of this proxy</typeparam>
private class ListCollectionViewProxy<T> : IList, IEnumerable<T>, INotifyCollectionChanged, ICollectionChangedListener where T : Entity
internal class ListCollectionViewProxy<T> : IList, IEnumerable<T>, INotifyCollectionChanged, ICollectionChangedListener where T : Entity
{
private readonly object _syncRoot = new object();
private readonly EntityCollection<T> _source;
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<City> entitySet;
EntityCollection<City> entityCollection = this.CreateEntityCollection(out entitySet);
EntityCollection<City>.ListCollectionViewProxy<City> 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()
Expand Down

0 comments on commit ac5c1de

Please sign in to comment.