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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
namespace System.Web.ModelBinding {
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
public class ModelBindingContext {
private ModelBinderProviderCollection _modelBinderProviders;
private ModelMetadata _modelMetadata;
private string _modelName;
private ModelStateDictionary _modelState;
private Dictionary<string, ModelMetadata> _propertyMetadata;
private ModelValidationNode _validationNode;
private IValueProvider _valueProvider;
public ModelBindingContext()
: this(null) {
ValidateRequest = true;
}
// copies certain values that won't change between parent and child objects,
// e.g. ValueProvider, ModelState
public ModelBindingContext(ModelBindingContext bindingContext) {
if (bindingContext != null) {
ModelBinderProviders = bindingContext.ModelBinderProviders;
ModelState = bindingContext.ModelState;
ValueProvider = bindingContext.ValueProvider;
ValidateRequest = bindingContext.ValidateRequest;
}
}
public object Model {
get {
EnsureModelMetadata();
return ModelMetadata.Model;
}
set {
EnsureModelMetadata();
ModelMetadata.Model = value;
}
}
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "This is writeable to support unit testing")]
public ModelBinderProviderCollection ModelBinderProviders {
get {
if (_modelBinderProviders == null) {
_modelBinderProviders = System.Web.ModelBinding.ModelBinderProviders.Providers;
}
return _modelBinderProviders;
}
set {
_modelBinderProviders = value;
}
}
public ModelMetadata ModelMetadata {
get {
return _modelMetadata;
}
set {
_modelMetadata = value;
}
}
public string ModelName {
get {
if (_modelName == null) {
_modelName = String.Empty;
}
return _modelName;
}
set {
_modelName = value;
}
}
public bool ValidateRequest {
get;
set;
}
[SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "This is writeable to support unit testing")]
public ModelStateDictionary ModelState {
get {
if (_modelState == null) {
_modelState = new ModelStateDictionary();
}
return _modelState;
}
set {
_modelState = value;
}
}
public Type ModelType {
get {
EnsureModelMetadata();
return ModelMetadata.ModelType;
}
}
public IDictionary<string, ModelMetadata> PropertyMetadata {
get {
if (_propertyMetadata == null) {
_propertyMetadata = ModelMetadata.Properties.ToDictionary(m => m.PropertyName, StringComparer.OrdinalIgnoreCase);
}
return _propertyMetadata;
}
}
public ModelValidationNode ValidationNode {
get {
if (_validationNode == null) {
_validationNode = new ModelValidationNode(ModelMetadata, ModelName);
}
return _validationNode;
}
set {
_validationNode = value;
}
}
public IValueProvider ValueProvider {
get {
return _valueProvider;
}
set {
_valueProvider = value;
}
}
internal IUnvalidatedValueProvider UnvalidatedValueProvider {
get {
return (ValueProvider as IUnvalidatedValueProvider ?? new UnvalidatedValueProviderWrapper(ValueProvider));
}
}
private void EnsureModelMetadata() {
if (ModelMetadata == null) {
throw Error.ModelBindingContext_ModelMetadataMustBeSet();
}
}
// Used to wrap an IValueProvider in an IUnvalidatedValueProvider
private sealed class UnvalidatedValueProviderWrapper : IValueProvider, IUnvalidatedValueProvider {
private readonly IValueProvider _backingProvider;
public UnvalidatedValueProviderWrapper(IValueProvider backingProvider) {
_backingProvider = backingProvider;
}
public ValueProviderResult GetValue(string key, bool skipValidation) {
// 'skipValidation' isn't understood by the backing provider and can be ignored
return GetValue(key);
}
public bool ContainsPrefix(string prefix) {
return _backingProvider.ContainsPrefix(prefix);
}
public ValueProviderResult GetValue(string key) {
return _backingProvider.GetValue(key);
}
}
}
}
|