// Copyright (c) Microsoft Open Technologies, Inc. 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.Linq;
using Microsoft.AspNet.Http;
namespace Microsoft.AspNet.Mvc.ModelBinding
{
///
/// A context that contains operating information for model binding and validation.
///
public class ModelBindingContext
{
private static readonly Func
_defaultPropertyFilter = (context, propertyName) => true;
private string _modelName;
private ModelStateDictionary _modelState;
private Dictionary _propertyMetadata;
private ModelValidationNode _validationNode;
private Func _propertyFilter;
///
/// Initializes a new instance of the class.
///
public ModelBindingContext()
{
}
///
/// Initializes a new instance of the class using the
/// .
///
/// Existing .
/// Model name of associated with the new .
/// Model metadata of associated with the new .
///
///
/// This constructor copies certain values that won't change between parent and child objects,
/// e.g. ValueProvider, ModelState
///
public ModelBindingContext([NotNull] ModelBindingContext bindingContext,
[NotNull] string modelName,
[NotNull] ModelMetadata modelMetadata)
{
ModelName = modelName;
ModelMetadata = modelMetadata;
ModelState = bindingContext.ModelState;
ValueProvider = bindingContext.ValueProvider;
OperationBindingContext = bindingContext.OperationBindingContext;
}
///
/// Represents the associated with this context.
///
public OperationBindingContext OperationBindingContext { get; set; }
///
/// Gets or sets the model associated with this context.
///
///
/// The property must be set to access this property.
///
public object Model
{
get
{
EnsureModelMetadata();
return ModelMetadata.Model;
}
set
{
EnsureModelMetadata();
ModelMetadata.Model = value;
}
}
///
/// Gets or sets the metadata for the model associated with this context.
///
public ModelMetadata ModelMetadata { get; set; }
///
/// Gets or sets the name of the model. This property is used as a key for looking up values in
/// during model binding.
///
public string ModelName
{
get
{
if (_modelName == null)
{
_modelName = string.Empty;
}
return _modelName;
}
set { _modelName = value; }
}
///
/// Gets or sets the used to capture values
/// for properties in the object graph of the model when binding.
///
public ModelStateDictionary ModelState
{
get
{
if (_modelState == null)
{
_modelState = new ModelStateDictionary();
}
return _modelState;
}
set { _modelState = value; }
}
///
/// Gets the type of the model.
///
///
/// The property must be set to access this property.
///
public Type ModelType
{
get
{
EnsureModelMetadata();
return ModelMetadata.ModelType;
}
}
///
/// Gets or sets a value that indicates whether the binder should use an empty prefix to look up
/// values in when no values are found using the
/// prefix.
///
public bool FallbackToEmptyPrefix { get; set; }
///
/// Gets or sets the associated with this context.
///
public IValueProvider ValueProvider { get; set; }
///
/// Gets a dictionary of property name to instances for
///
///
public IDictionary PropertyMetadata
{
get
{
if (_propertyMetadata == null)
{
_propertyMetadata = ModelMetadata.Properties
.ToDictionary(m => m.PropertyName,
StringComparer.OrdinalIgnoreCase);
}
return _propertyMetadata;
}
}
public Func PropertyFilter
{
get
{
if (_propertyFilter == null)
{
_propertyFilter = _defaultPropertyFilter;
}
return _propertyFilter;
}
set { _propertyFilter = value; }
}
///
/// Gets or sets the instance used as a container for
/// validation information.
///
public ModelValidationNode ValidationNode
{
get
{
if (_validationNode == null)
{
_validationNode = new ModelValidationNode(ModelMetadata, ModelName);
}
return _validationNode;
}
set { _validationNode = value; }
}
private void EnsureModelMetadata()
{
if (ModelMetadata == null)
{
throw new InvalidOperationException(Resources.ModelBindingContext_ModelMetadataMustBeSet);
}
}
}
}