Skip to content

Commit

Permalink
ISourceCache.KeySelector (reactivemarbles#374)
Browse files Browse the repository at this point in the history
* ISourceCache.KeySelector

Adds a getter to retrieve the KeySelector func that a cache uses for comparison.  Will help users more accurately know what a cache is using to determine its key, and utilize it in logic that needs to match in key selection

* Merged KeySelector initialization with null check
  • Loading branch information
Noggog committed Jun 26, 2020
1 parent 1e61faa commit d213d2e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/DynamicData/Cache/ISourceCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@ public interface ISourceCache<TObject, TKey> : IObservableCache<TObject, TKey>
/// </summary>
/// <param name="updateAction">The update action.</param>
void Edit(Action<ISourceUpdater<TObject, TKey>> updateAction);

/// <summary>
/// Key selector used by the cache to retrieve keys from objects
/// </summary>
Func<TObject, TKey> KeySelector { get; }
}
}
8 changes: 3 additions & 5 deletions src/DynamicData/Cache/SourceCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ public class SourceCache<TObject, TKey> : ISourceCache<TObject, TKey>
/// <exception cref="System.ArgumentNullException">keySelector</exception>
public SourceCache(Func<TObject, TKey> keySelector)
{
if (keySelector == null)
{
throw new ArgumentNullException(nameof(keySelector));
}

KeySelector = keySelector ?? throw new ArgumentNullException(nameof(keySelector));
_innerCache = new ObservableCache<TObject, TKey>(keySelector);
}

Expand Down Expand Up @@ -62,6 +58,8 @@ public SourceCache(Func<TObject, TKey> keySelector)
/// <inheritdoc />
public IEnumerable<TKey> Keys => _innerCache.Keys;

public Func<TObject, TKey> KeySelector { get; }

/// <inheritdoc />
public Optional<TObject> Lookup(TKey key) => _innerCache.Lookup(key);

Expand Down

0 comments on commit d213d2e

Please sign in to comment.