From bd577975b2d5d53c5c90de8f1d5d181930b5dd08 Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Fri, 27 Sep 2024 19:55:45 +0100 Subject: [PATCH] Allow validation to be looked up by name --- .../IReadOnlyIndexDictionary.cs | 10 ++++ Toolkit.Foundation/IValidation.cs | 2 +- Toolkit.Foundation/ReadOnlyIndexDictionary.cs | 56 +++++++++++++++++++ Toolkit.Foundation/Validation.cs | 5 +- 4 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 Toolkit.Foundation/IReadOnlyIndexDictionary.cs create mode 100644 Toolkit.Foundation/ReadOnlyIndexDictionary.cs diff --git a/Toolkit.Foundation/IReadOnlyIndexDictionary.cs b/Toolkit.Foundation/IReadOnlyIndexDictionary.cs new file mode 100644 index 0000000..3b7a0f2 --- /dev/null +++ b/Toolkit.Foundation/IReadOnlyIndexDictionary.cs @@ -0,0 +1,10 @@ + +namespace Toolkit.Foundation +{ + public interface IReadOnlyIndexDictionary : + IReadOnlyDictionary + where TKey : notnull + { + KeyValuePair this[int index] { get; } + } +} \ No newline at end of file diff --git a/Toolkit.Foundation/IValidation.cs b/Toolkit.Foundation/IValidation.cs index fc8d8ab..94cb1e8 100644 --- a/Toolkit.Foundation/IValidation.cs +++ b/Toolkit.Foundation/IValidation.cs @@ -6,7 +6,7 @@ namespace Toolkit.Foundation; public interface IValidation : INotifyPropertyChanged { - IReadOnlyDictionary Errors { get; } + IReadOnlyIndexDictionary Errors { get; } bool HasErrors { get; } diff --git a/Toolkit.Foundation/ReadOnlyIndexDictionary.cs b/Toolkit.Foundation/ReadOnlyIndexDictionary.cs new file mode 100644 index 0000000..f88c555 --- /dev/null +++ b/Toolkit.Foundation/ReadOnlyIndexDictionary.cs @@ -0,0 +1,56 @@ +using System.Collections; +using System.Diagnostics.CodeAnalysis; + +namespace Toolkit.Foundation; + +public class ReadOnlyIndexDictionary : + IReadOnlyDictionary, + IReadOnlyIndexDictionary + where TKey : notnull +{ + private readonly Dictionary dictionary; + private readonly List> indexedItems; + + public ReadOnlyIndexDictionary(IDictionary items) + { + dictionary = new Dictionary(items); + indexedItems = [.. dictionary]; + } + + public TValue this[TKey key] => + dictionary[key]; + + public KeyValuePair this[int index] => + indexedItems[index]; + + public IEnumerable Keys => + dictionary.Keys; + + public IEnumerable Values => + dictionary.Values; + + public int Count => dictionary.Count; + + public bool ContainsKey(TKey key) => + dictionary.ContainsKey(key); + + public IEnumerator> GetEnumerator() => + dictionary.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => dictionary.GetEnumerator(); + + public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) + { + if (dictionary.TryGetValue(key, out TValue? localValue)) + { + if (localValue is not null) + { + value = localValue; + return true; + } + } + + value = default; + return false; + } +} diff --git a/Toolkit.Foundation/Validation.cs b/Toolkit.Foundation/Validation.cs index c5337c0..778c17e 100644 --- a/Toolkit.Foundation/Validation.cs +++ b/Toolkit.Foundation/Validation.cs @@ -10,8 +10,8 @@ public class Validation(IValidatorCollection validators) : public event PropertyChangedEventHandler? PropertyChanged; - public IReadOnlyDictionary Errors => - errors.AsReadOnly(); + public IReadOnlyIndexDictionary Errors => + new ReadOnlyIndexDictionary(errors); public bool HasErrors => Errors.Count > 0; @@ -116,6 +116,7 @@ public class Validation(IValidatorCollection validators) : OnPropertyChanged(nameof(Errors), null, null); } } + private string GetPropertyName(Expression> expression) { return expression.Body switch