-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collection change tracking for primitive collections
Part of #25364
- Loading branch information
1 parent
bd16563
commit 812d6c8
Showing
14 changed files
with
1,016 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.EntityFrameworkCore.ChangeTracking; | ||
|
||
/// <summary> | ||
/// A <see cref="ValueComparer{T}"/> for lists of primitive items. The list can be typed as <see cref="IEnumerable{T}"/>, | ||
/// but can only be used with instances that implement <see cref="IList{T}"/>. | ||
/// </summary> | ||
/// <remarks> | ||
/// <para> | ||
/// This comparer should be used for reference types and non-nullable value types. Use | ||
/// <see cref="NullableValueTypeListComparer{TElement}"/> for nullable value types. | ||
/// </para> | ||
/// <para> | ||
/// See <see href="https://aka.ms/efcore-docs-value-comparers">EF Core value comparers</see> for more information and examples. | ||
/// </para> | ||
/// </remarks> | ||
/// <typeparam name="TElement">The element type.</typeparam> | ||
public sealed class ListComparer<TElement> : ValueComparer<IEnumerable<TElement>> | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of the list comparer. | ||
/// </summary> | ||
/// <param name="elementComparer">The comparer to use for comparing elements.</param> | ||
public ListComparer(ValueComparer<TElement> elementComparer) | ||
: base( | ||
(a, b) => Compare(a, b, elementComparer), | ||
o => GetHashCode(o, elementComparer), | ||
source => Snapshot(source, elementComparer)) | ||
{ | ||
} | ||
|
||
private static bool Compare(IEnumerable<TElement>? a, IEnumerable<TElement>? b, ValueComparer<TElement> elementComparer) | ||
{ | ||
if (ReferenceEquals(a, b)) | ||
{ | ||
return true; | ||
} | ||
|
||
if (a is null) | ||
{ | ||
return b is null; | ||
} | ||
|
||
if (b is null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (a is IList<TElement> aList && b is IList<TElement> bList) | ||
{ | ||
if (aList.Count != bList.Count) | ||
{ | ||
return false; | ||
} | ||
|
||
for (var i = 0; i < aList.Count; i++) | ||
{ | ||
var (el1, el2) = (aList[i], bList[i]); | ||
if (el1 is null) | ||
{ | ||
if (el2 is null) | ||
{ | ||
continue; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
if (el2 is null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (!elementComparer.Equals(el1, el2)) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
throw new InvalidOperationException( | ||
CoreStrings.BadListType( | ||
(a is IList<TElement?> ? b : a).GetType().ShortDisplayName(), | ||
typeof(ListComparer<TElement?>).ShortDisplayName(), | ||
typeof(IList<>).MakeGenericType(elementComparer.Type).ShortDisplayName())); | ||
} | ||
|
||
private static int GetHashCode(IEnumerable<TElement> source, ValueComparer<TElement> elementComparer) | ||
{ | ||
var hash = new HashCode(); | ||
|
||
foreach (var el in source) | ||
{ | ||
hash.Add(el == null ? 0 : elementComparer.GetHashCode(el)); | ||
} | ||
|
||
return hash.ToHashCode(); | ||
} | ||
|
||
private static IList<TElement> Snapshot(IEnumerable<TElement> source, ValueComparer<TElement> elementComparer) | ||
{ | ||
if (!(source is IList<TElement> sourceList)) | ||
{ | ||
throw new InvalidOperationException( | ||
CoreStrings.BadListType( | ||
source.GetType().ShortDisplayName(), | ||
typeof(ListComparer<TElement?>).ShortDisplayName(), | ||
typeof(IList<>).MakeGenericType(elementComparer.Type).ShortDisplayName())); | ||
} | ||
|
||
if (sourceList.GetType().IsArray) | ||
{ | ||
var snapshot = new TElement[sourceList.Count]; | ||
|
||
for (var i = 0; i < sourceList.Count; i++) | ||
{ | ||
var instance = sourceList[i]; | ||
if (instance != null) | ||
{ | ||
snapshot[i] = elementComparer.Snapshot(instance); | ||
} | ||
} | ||
|
||
return snapshot; | ||
} | ||
else | ||
{ | ||
var snapshot = source is List<TElement> | ||
? new List<TElement>(sourceList.Count) | ||
: (IList<TElement>)Activator.CreateInstance(source.GetType())!; | ||
|
||
foreach (var e in sourceList) | ||
{ | ||
snapshot.Add(e == null ? (TElement)(object)null! : elementComparer.Snapshot(e)); | ||
} | ||
|
||
return snapshot; | ||
} | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
src/EFCore/ChangeTracking/NullableValueTypeListComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.EntityFrameworkCore.ChangeTracking; | ||
|
||
/// <summary> | ||
/// A <see cref="ValueComparer{T}"/> for lists of primitive items. The list can be typed as <see cref="IEnumerable{T}"/>, | ||
/// but can only be used with instances that implement <see cref="IList{T}"/>. | ||
/// </summary> | ||
/// <remarks> | ||
/// <para> | ||
/// This comparer should be used for nullable value types. Use <see cref="NullableValueTypeListComparer{TElement}"/> for reference | ||
/// types and non-nullable value types. | ||
/// </para> | ||
/// <para> | ||
/// See <see href="https://aka.ms/efcore-docs-value-comparers">EF Core value comparers</see> for more information and examples. | ||
/// </para> | ||
/// </remarks> | ||
/// <typeparam name="TElement">The element type.</typeparam> | ||
public sealed class NullableValueTypeListComparer<TElement> : ValueComparer<IEnumerable<TElement?>> | ||
where TElement : struct | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of the list comparer. | ||
/// </summary> | ||
/// <param name="elementComparer">The comparer to use for comparing elements.</param> | ||
public NullableValueTypeListComparer(ValueComparer<TElement> elementComparer) | ||
: base( | ||
(a, b) => Compare(a, b, elementComparer), | ||
o => GetHashCode(o, elementComparer), | ||
source => Snapshot(source, elementComparer)) | ||
{ | ||
} | ||
|
||
private static bool Compare(IEnumerable<TElement?>? a, IEnumerable<TElement?>? b, ValueComparer<TElement> elementComparer) | ||
{ | ||
if (ReferenceEquals(a, b)) | ||
{ | ||
return true; | ||
} | ||
|
||
if (a is null) | ||
{ | ||
return b is null; | ||
} | ||
|
||
if (b is null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (a is IList<TElement?> aList && b is IList<TElement?> bList) | ||
{ | ||
if (aList.Count != bList.Count) | ||
{ | ||
return false; | ||
} | ||
|
||
for (var i = 0; i < aList.Count; i++) | ||
{ | ||
var (el1, el2) = (aList[i], bList[i]); | ||
if (el1 is null) | ||
{ | ||
if (el2 is null) | ||
{ | ||
continue; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
if (el2 is null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (!elementComparer.Equals(el1, el2)) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
throw new InvalidOperationException( | ||
CoreStrings.BadListType( | ||
(a is IList<TElement?> ? b : a).GetType().ShortDisplayName(), | ||
typeof(NullableValueTypeListComparer<TElement>).ShortDisplayName(), | ||
typeof(IList<>).MakeGenericType(elementComparer.Type).ShortDisplayName())); | ||
} | ||
|
||
private static int GetHashCode(IEnumerable<TElement?> source, ValueComparer<TElement> elementComparer) | ||
{ | ||
var hash = new HashCode(); | ||
|
||
foreach (var el in source) | ||
{ | ||
hash.Add(el == null ? 0 : elementComparer.GetHashCode(el)); | ||
} | ||
|
||
return hash.ToHashCode(); | ||
} | ||
|
||
private static IList<TElement?> Snapshot(IEnumerable<TElement?> source, ValueComparer<TElement> elementComparer) | ||
{ | ||
if (!(source is IList<TElement?> sourceList)) | ||
{ | ||
throw new InvalidOperationException( | ||
CoreStrings.BadListType( | ||
source.GetType().ShortDisplayName(), | ||
typeof(NullableValueTypeListComparer<TElement>).ShortDisplayName(), | ||
typeof(IList<>).MakeGenericType(elementComparer.Type).ShortDisplayName())); | ||
} | ||
|
||
if (sourceList.GetType().IsArray) | ||
{ | ||
var snapshot = new TElement?[sourceList.Count]; | ||
|
||
for (var i = 0; i < sourceList.Count; i++) | ||
{ | ||
var instance = sourceList[i]; | ||
snapshot[i] = instance == null ? null : (TElement?)elementComparer.Snapshot(instance); | ||
} | ||
|
||
return snapshot; | ||
} | ||
else | ||
{ | ||
var snapshot = source is List<TElement?> | ||
? new List<TElement?>(sourceList.Count) | ||
: (IList<TElement?>)Activator.CreateInstance(source.GetType())!; | ||
|
||
foreach (var e in sourceList) | ||
{ | ||
snapshot.Add(e == null ? null : (TElement?)elementComparer.Snapshot(e)); | ||
} | ||
|
||
return snapshot; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.