Skip to content

Make persistent collection classes implement the IReadOnly* interfaces #2284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/NHibernate.Test/CollectionTest/PersistentCollectionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,22 @@ public void AddRangeWorksCorrectly()

Assert.That(items, Is.EqualTo(new[] {"A", "B", "C", "D", "E"}));
}

[Test]
public void SelectManyWorksCorrectlyWithIReadOnlyCollection()
{
var bags = new IReadOnlyCollection<string>[]
{
new List<string> {"A"},
new PersistentGenericBag<string>(null, new[] {"B"}),
new PersistentIdentifierBag<string>(null, new[] {"C"}),
(IReadOnlyList<string>)new PersistentGenericList<string>(null, new[] {"D"}),
new PersistentGenericSet<string>(null, new HashSet<string> {"E"})
};

var items = bags.SelectMany(b => b).ToArray();

Assert.That(items, Is.EqualTo(new[] {"A", "B", "C", "D", "E"}));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace NHibernate.Collection.Generic
{
using System.Threading.Tasks;
using System.Threading;
public partial class PersistentGenericBag<T> : AbstractPersistentCollection, IList<T>, IList, IQueryable<T>
public partial class PersistentGenericBag<T> : AbstractPersistentCollection, IList<T>, IReadOnlyList<T>, IList, IQueryable<T>
{

public override async Task<object> DisassembleAsync(ICollectionPersister persister, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace NHibernate.Collection.Generic
{
using System.Threading.Tasks;
using System.Threading;
public partial class PersistentIdentifierBag<T> : AbstractPersistentCollection, IList<T>, IList, IQueryable<T>
public partial class PersistentIdentifierBag<T> : AbstractPersistentCollection, IList<T>, IReadOnlyList<T>, IList, IQueryable<T>
{

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace NHibernate.Collection.Generic
{
using System.Threading.Tasks;
using System.Threading;
public partial class PersistentGenericList<T> : AbstractPersistentCollection, IList<T>, IList, IQueryable<T>
public partial class PersistentGenericList<T> : AbstractPersistentCollection, IList<T>, IReadOnlyList<T>, IList, IQueryable<T>
{

//Since 5.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace NHibernate.Collection.Generic
{
using System.Threading.Tasks;
using System.Threading;
public partial class PersistentGenericMap<TKey, TValue> : AbstractPersistentCollection, IDictionary<TKey, TValue>, ICollection
public partial class PersistentGenericMap<TKey, TValue> : AbstractPersistentCollection, IDictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue>, ICollection
{

//Since 5.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace NHibernate.Collection.Generic
{
using System.Threading.Tasks;
using System.Threading;
public partial class PersistentGenericSet<T> : AbstractPersistentCollection, ISet<T>, IQueryable<T>
public partial class PersistentGenericSet<T> : AbstractPersistentCollection, ISet<T>, IReadOnlyCollection<T>, IQueryable<T>
{

//Since 5.3
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Collection/Generic/PersistentGenericBag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace NHibernate.Collection.Generic
/// <remarks>The underlying collection used is an <see cref="List{T}"/></remarks>
[Serializable]
[DebuggerTypeProxy(typeof (CollectionProxy<>))]
public partial class PersistentGenericBag<T> : AbstractPersistentCollection, IList<T>, IList, IQueryable<T>
public partial class PersistentGenericBag<T> : AbstractPersistentCollection, IList<T>, IReadOnlyList<T>, IList, IQueryable<T>
{
// TODO NH: find a way to writeonce (no duplicated code from PersistentBag)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace NHibernate.Collection.Generic
/// </remarks>
[Serializable]
[DebuggerTypeProxy(typeof (CollectionProxy<>))]
public partial class PersistentIdentifierBag<T> : AbstractPersistentCollection, IList<T>, IList, IQueryable<T>
public partial class PersistentIdentifierBag<T> : AbstractPersistentCollection, IList<T>, IReadOnlyList<T>, IList, IQueryable<T>
{
/* NH considerations:
* For various reason we know that the underlining type will be a List<T> or a
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Collection/Generic/PersistentGenericList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace NHibernate.Collection.Generic
/// <remarks>The underlying collection used is a <see cref="List{T}"/></remarks>
[Serializable]
[DebuggerTypeProxy(typeof (CollectionProxy<>))]
public partial class PersistentGenericList<T> : AbstractPersistentCollection, IList<T>, IList, IQueryable<T>
public partial class PersistentGenericList<T> : AbstractPersistentCollection, IList<T>, IReadOnlyList<T>, IList, IQueryable<T>
{
protected IList<T> WrappedList;

Expand Down
12 changes: 11 additions & 1 deletion src/NHibernate/Collection/Generic/PersistentGenericMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace NHibernate.Collection.Generic
/// <typeparam name="TValue">The type of the elements in the IDictionary.</typeparam>
[Serializable]
[DebuggerTypeProxy(typeof(DictionaryProxy<,>))]
public partial class PersistentGenericMap<TKey, TValue> : AbstractPersistentCollection, IDictionary<TKey, TValue>, ICollection
public partial class PersistentGenericMap<TKey, TValue> : AbstractPersistentCollection, IDictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue>, ICollection
{
protected IDictionary<TKey, TValue> WrappedMap;
private readonly ICollection<TValue> _wrappedValues;
Expand Down Expand Up @@ -360,6 +360,16 @@ public ICollection<TValue> Values
}
}

IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys
{
get { return Keys; }
}

IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values
{
get { return Values; }
}

#endregion

#region ICollection<KeyValuePair<TKey,TValue>> Members
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Collection/Generic/PersistentGenericSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace NHibernate.Collection.Generic
/// </summary>
[Serializable]
[DebuggerTypeProxy(typeof(CollectionProxy<>))]
public partial class PersistentGenericSet<T> : AbstractPersistentCollection, ISet<T>, IQueryable<T>
public partial class PersistentGenericSet<T> : AbstractPersistentCollection, ISet<T>, IReadOnlyCollection<T>, IQueryable<T>
{
/// <summary>
/// The <see cref="ISet{T}"/> that NHibernate is wrapping.
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate/Collection/Generic/SetHelpers/ISetSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace NHibernate.Collection.Generic.SetHelpers
{
internal interface ISetSnapshot<T> : ICollection<T>, ICollection
internal interface ISetSnapshot<T> : ICollection<T>, IReadOnlyCollection<T>, ICollection
{
bool TryGetValue(T element, out T value);
}
}
}
7 changes: 6 additions & 1 deletion src/NHibernate/Collection/Generic/SetHelpers/SetSnapShot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ int ICollection<T>.Count
get { return _elements.Count; }
}

int IReadOnlyCollection<T>.Count
{
get { return _elements.Count; }
}

public bool IsReadOnly
{
get { return ((ICollection<T>)_elements).IsReadOnly; }
Expand All @@ -101,4 +106,4 @@ public bool TryGetValue(T element, out T value)
return false;
}
}
}
}