// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Globalization; namespace Microsoft.AspNet.Mvc.ModelBinding { /// /// An adapter for data stored in an /// . /// public class DictionaryBasedValueProvider : BindingSourceValueProvider { private readonly IDictionary _values; private PrefixContainer _prefixContainer; /// /// Creates a new . /// /// The of the data. /// The values. public DictionaryBasedValueProvider( BindingSource bindingSource, IDictionary values) : base(bindingSource) { if (bindingSource == null) { throw new ArgumentNullException(nameof(bindingSource)); } if (values == null) { throw new ArgumentNullException(nameof(values)); } _values = values; } protected PrefixContainer PrefixContainer { get { if (_prefixContainer == null) { _prefixContainer = new PrefixContainer(_values.Keys); } return _prefixContainer; } } /// public override bool ContainsPrefix(string key) { return PrefixContainer.ContainsPrefix(key); } /// public override ValueProviderResult GetValue(string key) { if (key == null) { throw new ArgumentNullException(nameof(key)); } object value; if (_values.TryGetValue(key, out value)) { var stringValue = value as string ?? value?.ToString() ?? string.Empty; return new ValueProviderResult(stringValue, CultureInfo.InvariantCulture); } else { return ValueProviderResult.None; } } } }