From 8e495158ef8ecb6e7e0263b48405196d05d5f2be Mon Sep 17 00:00:00 2001 From: Pranav K Date: Wed, 26 Mar 2014 11:14:31 -0700 Subject: [PATCH] Modifying ModelStateDictionary to derive from IDictionary * Reintroducing methods that weren't ported over earlier --- .../ModelStateDictionary.cs | 120 +++++++++++++++++- 1 file changed, 113 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.ModelBinding/ModelStateDictionary.cs b/src/Microsoft.AspNet.Mvc.ModelBinding/ModelStateDictionary.cs index 52b09b705f..0dfbf7e8c7 100644 --- a/src/Microsoft.AspNet.Mvc.ModelBinding/ModelStateDictionary.cs +++ b/src/Microsoft.AspNet.Mvc.ModelBinding/ModelStateDictionary.cs @@ -1,13 +1,14 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using Microsoft.AspNet.Mvc.ModelBinding.Internal; namespace Microsoft.AspNet.Mvc.ModelBinding { - public class ModelStateDictionary : Dictionary + public class ModelStateDictionary : IDictionary { - private readonly Dictionary _innerDictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); + private readonly IDictionary _innerDictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); public ModelStateDictionary() { @@ -21,17 +22,50 @@ namespace Microsoft.AspNet.Mvc.ModelBinding } } + #region IDictionary properties + public int Count + { + get { return _innerDictionary.Count; } + } + + public bool IsReadOnly + { + get { return _innerDictionary.IsReadOnly; } + } + + public ICollection Keys + { + get { return _innerDictionary.Keys; } + } + + public ICollection Values + { + get { return _innerDictionary.Values; } + } + #endregion + public bool IsValid { get { return Values.All(modelState => modelState.Errors.Count == 0); } } - public void AddModelError(string key, Exception exception) + public ModelState this[[NotNull] string key] + { + get + { + ModelState value; + _innerDictionary.TryGetValue(key, out value); + return value; + } + set { _innerDictionary[key] = value; } + } + + public void AddModelError([NotNull] string key, [NotNull] Exception exception) { GetModelStateForKey(key).Errors.Add(exception); } - public void AddModelError(string key, string errorMessage) + public void AddModelError([NotNull] string key, [NotNull] string errorMessage) { GetModelStateForKey(key).Errors.Add(errorMessage); } @@ -39,7 +73,7 @@ namespace Microsoft.AspNet.Mvc.ModelBinding public bool IsValidField([NotNull] string key) { // if the key is not found in the dictionary, we just say that it's valid (since there are no errors) - foreach (var entry in DictionaryHelper.FindKeysWithPrefix(this, key)) + foreach (var entry in DictionaryHelper.FindKeysWithPrefix(_innerDictionary, key)) { if (entry.Value.Errors.Count != 0) { @@ -49,6 +83,24 @@ namespace Microsoft.AspNet.Mvc.ModelBinding return true; } + public void Merge(ModelStateDictionary dictionary) + { + if (dictionary == null) + { + return; + } + + foreach (var entry in dictionary) + { + this[entry.Key] = entry.Value; + } + } + + public void SetModelValue([NotNull] string key, [NotNull] ValueProviderResult value) + { + GetModelStateForKey(key).Value = value; + } + private ModelState GetModelStateForKey([NotNull] string key) { ModelState modelState; @@ -60,9 +112,63 @@ namespace Microsoft.AspNet.Mvc.ModelBinding return modelState; } - public void SetModelValue(string key, ValueProviderResult value) + + #region IDictionary members + public void Add(KeyValuePair item) { - GetModelStateForKey(key).Value = value; + _innerDictionary.Add(item); } + + public void Add([NotNull] string key, [NotNull] ModelState value) + { + _innerDictionary.Add(key, value); + } + + public void Clear() + { + _innerDictionary.Clear(); + } + + public bool Contains(KeyValuePair item) + { + return _innerDictionary.Contains(item); + } + + public bool ContainsKey([NotNull] string key) + { + return _innerDictionary.ContainsKey(key); + } + + public void CopyTo([NotNull] KeyValuePair[] array, int arrayIndex) + { + _innerDictionary.CopyTo(array, arrayIndex); + } + + + public bool Remove(KeyValuePair item) + { + return _innerDictionary.Remove(item); + } + + public bool Remove([NotNull] string key) + { + return _innerDictionary.Remove(key); + } + + public bool TryGetValue([NotNull] string key, out ModelState value) + { + return _innerDictionary.TryGetValue(key, out value); + } + + public IEnumerator> GetEnumerator() + { + return _innerDictionary.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + #endregion } } \ No newline at end of file