Repository navigation
Expand file tree
/
Copy pathCacheChangeSetEx.cs
More file actions
57 lines (51 loc) · 2.89 KB
/
Copy pathCacheChangeSetEx.cs
File metadata and controls
57 lines (51 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) 2011-2023 Roland Pheasant. All rights reserved.
// Roland Pheasant licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using DynamicData.Kernel;
namespace DynamicData.Cache;
internal static class CacheChangeSetEx
{
/// <summary>
/// <para>
/// IChangeSet is flawed because it automatically means allocations when enumerating.
/// This extension is a crazy hack to cast to the concrete change set which means we no longer allocate
/// as change set now inherits from List which has allocation free enumerations.
/// </para>
/// <para>IChangeSet will be removed in a future version and instead <see cref="ChangeSet{TObject, TKey}"/> will be used directly.</para>
/// <para>In the mean time I am banking that no-one has implemented a custom change set - personally I think it is very unlikely.</para>
/// </summary>
/// <typeparam name="TObject">ChangeSet Object Type.</typeparam>
/// <typeparam name="TKey">ChangeSet Key Type.</typeparam>
/// <param name="changeSet">ChangeSet to be converted.</param>
/// <returns>Concrete Instance of the ChangeSet.</returns>
/// <exception cref="NotSupportedException">A custom implementation was found.</exception>
public static ChangeSet<TObject, TKey> ToConcreteType<TObject, TKey>(this IChangeSet<TObject, TKey> changeSet)
where TObject : notnull
where TKey : notnull =>
changeSet as ChangeSet<TObject, TKey> ?? throw new NotSupportedException("Dynamic Data does not support a custom implementation of IChangeSet");
/// <summary>
/// Transforms the change set into a different type using the specified transform function.
/// </summary>
/// <typeparam name="TSource">The type of the source.</typeparam>
/// <typeparam name="TDestination">The type of the destination.</typeparam>
/// <typeparam name="TKey">The type of the Key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="transformer">The transformer.</param>
/// <returns>The change set.</returns>
/// <exception cref="ArgumentNullException">
/// source
/// or
/// transformer.
/// </exception>
public static IChangeSet<TDestination, TKey> Transform<TSource, TDestination, TKey>(this IChangeSet<TSource, TKey> source, Func<TSource, TDestination> transformer)
where TSource : notnull
where TDestination : notnull
where TKey : notnull
{
source.ThrowArgumentNullExceptionIfNull(nameof(source));
transformer.ThrowArgumentNullExceptionIfNull(nameof(transformer));
var changes = source.Select(change =>
new Change<TDestination, TKey>(change.Reason, change.Key, transformer(change.Current), change.Previous.Convert(transformer), change.CurrentIndex, change.PreviousIndex));
return new ChangeSet<TDestination, TKey>(changes);
}
}