diff --git a/Remotion/Data/DomainObjects.UnitTests/ClientTransactionExtensionCollectionTest.cs b/Remotion/Data/DomainObjects.UnitTests/ClientTransactionExtensionCollectionTest.cs index fe833df909..833baf1a86 100644 --- a/Remotion/Data/DomainObjects.UnitTests/ClientTransactionExtensionCollectionTest.cs +++ b/Remotion/Data/DomainObjects.UnitTests/ClientTransactionExtensionCollectionTest.cs @@ -15,6 +15,7 @@ // along with re-motion; if not, see http://www.gnu.org/licenses. // using System; +using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; @@ -69,6 +70,39 @@ public void Initialization () Assert.That(((IClientTransactionExtension)collection).Key, Is.EqualTo("abc")); } + [Test] + public void GetEnumerator_IEnumerableGeneric () + { + _collection.Add(_extension1.Object); + _collection.Add(_extension2.Object); + + using IEnumerator enumerator = ((IEnumerable)_collection).GetEnumerator(); + + Assert.That(enumerator.Current, Is.Null); + Assert.That(enumerator.MoveNext(), Is.True); + Assert.That(enumerator.Current, Is.EqualTo(_extension1.Object)); + Assert.That(enumerator.MoveNext(), Is.True); + Assert.That(enumerator.Current, Is.EqualTo(_extension2.Object)); + Assert.That(enumerator.MoveNext(), Is.False); + } + + [Test] + public void GetEnumerator_ForIEnumerable () + { + _collection.Add(_extension1.Object); + _collection.Add(_extension2.Object); + + // ReSharper disable once NotDisposedResource + IEnumerator enumerator = ((IEnumerable)_collection).GetEnumerator(); + + Assert.That(enumerator.Current, Is.Null); + Assert.That(enumerator.MoveNext(), Is.True); + Assert.That(enumerator.Current, Is.EqualTo(_extension1.Object)); + Assert.That(enumerator.MoveNext(), Is.True); + Assert.That(enumerator.Current, Is.EqualTo(_extension2.Object)); + Assert.That(enumerator.MoveNext(), Is.False); + } + [Test] public void Add () { @@ -95,10 +129,11 @@ public void Insert () [Test] public void Remove () { + _collection.Add(_extension2.Object); _collection.Add(_extension1.Object); - Assert.That(_collection.Count, Is.EqualTo(1)); + Assert.That(_collection.Count, Is.EqualTo(2)); _collection.Remove(_extension1.Object.Key); - Assert.That(_collection.Count, Is.EqualTo(0)); + Assert.That(_collection.Count, Is.EqualTo(1)); _collection.Remove(_extension1.Object.Key); //expectation: no exception } @@ -113,12 +148,13 @@ public void Indexer () } [Test] - public void IndexerWithName () + public void IndexerWithKey () { _collection.Add(_extension1.Object); _collection.Add(_extension2.Object); Assert.That(_collection[_extension1.Object.Key], Is.SameAs(_extension1.Object)); Assert.That(_collection[_extension2.Object.Key], Is.SameAs(_extension2.Object)); + Assert.That(_collection["unknown"], Is.Null); } [Test] @@ -144,7 +180,7 @@ public void AddWithDuplicateKey () } [Test] - public void InsertWithDuplicateName () + public void InsertWithDuplicateKey () { _collection.Insert(0, _extension1.Object); diff --git a/Remotion/Data/DomainObjects/ClientTransactionExtensionCollection.cs b/Remotion/Data/DomainObjects/ClientTransactionExtensionCollection.cs index d6c5d99a26..28c25e3231 100644 --- a/Remotion/Data/DomainObjects/ClientTransactionExtensionCollection.cs +++ b/Remotion/Data/DomainObjects/ClientTransactionExtensionCollection.cs @@ -15,6 +15,7 @@ // along with re-motion; if not, see http://www.gnu.org/licenses. // using System; +using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Linq; @@ -30,9 +31,10 @@ namespace Remotion.Data.DomainObjects /// A collection of s. /// [Serializable] - public class ClientTransactionExtensionCollection : CommonCollection, IClientTransactionExtension + public class ClientTransactionExtensionCollection : IClientTransactionExtension, IReadOnlyList { private readonly string _key; + private readonly List> _extensions = new(); public ClientTransactionExtensionCollection (string key) { @@ -53,7 +55,7 @@ public IClientTransactionExtension? this[string key] { ArgumentUtility.CheckNotNullOrEmpty("key", key); - return (IClientTransactionExtension?)BaseGetObject(key); + return _extensions.FirstOrDefault(e => KeyPredicate(e, key)).Value; } } @@ -64,7 +66,7 @@ public IClientTransactionExtension? this[string key] /// The of the given . public IClientTransactionExtension this[int index] { - get { return (IClientTransactionExtension)BaseGetObject(index); } + get { return _extensions[index].Value; } } string IClientTransactionExtension.Key @@ -86,10 +88,10 @@ public void Add (IClientTransactionExtension clientTransactionExtension) var key = clientTransactionExtension.Key; Assertion.IsNotNull(key, "IClientTransactionExtension.Key must not return null"); - if (BaseContainsKey(key)) + if (_extensions.Any(e => KeyPredicate(e, key))) throw new InvalidOperationException(string.Format("An extension with key '{0}' is already part of the collection.", key)); - BaseAdd(key, clientTransactionExtension); + _extensions.Add(new KeyValuePair(key, clientTransactionExtension)); } /// @@ -100,7 +102,7 @@ public void Remove (string key) { ArgumentUtility.CheckNotNullOrEmpty("key", key); - BaseRemove(key); + _extensions.RemoveAll(e => KeyPredicate(e, key)); } /// @@ -112,7 +114,7 @@ public int IndexOf (string key) { ArgumentUtility.CheckNotNullOrEmpty("key", key); - return BaseIndexOfKey(key); + return _extensions.FindIndex(e => KeyPredicate(e, key)); } /// @@ -130,12 +132,20 @@ public void Insert (int index, IClientTransactionExtension clientTransactionExte var key = clientTransactionExtension.Key; Assertion.IsNotNull(key, "IClientTransactionExtension.Key must not return null"); - if (BaseContainsKey(key)) + if (_extensions.Any(e => KeyPredicate(e, key))) throw new InvalidOperationException(string.Format("An extension with key '{0}' is already part of the collection.", key)); - BaseInsert(index, key, clientTransactionExtension); + _extensions.Insert(index, new KeyValuePair(key, clientTransactionExtension)); } + IEnumerator IEnumerable.GetEnumerator () => _extensions.Select(e => e.Value).GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator () => ((IEnumerable)this).GetEnumerator(); + + public int Count => _extensions.Count; + + private static bool KeyPredicate (KeyValuePair extension, string key) => string.Equals(extension.Key, key, StringComparison.Ordinal); + #region Notification methods [EditorBrowsable(EditorBrowsableState.Never)] @@ -212,6 +222,7 @@ public void ObjectsLoaded (ClientTransaction clientTransaction, IReadOnlyList unloadedDomainObjects) { ArgumentUtility.DebugCheckNotNull("unloadedDomainObjects", unloadedDomainObjects); @@ -220,6 +231,7 @@ public void ObjectsUnloading (ClientTransaction clientTransaction, IReadOnlyList this[i].ObjectsUnloading(clientTransaction, unloadedDomainObjects); } + [EditorBrowsable(EditorBrowsableState.Never)] public void ObjectsUnloaded (ClientTransaction clientTransaction, IReadOnlyList unloadedDomainObjects) { ArgumentUtility.DebugCheckNotNull("unloadedDomainObjects", unloadedDomainObjects);