// 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 Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
namespace Microsoft.AspNetCore.Mvc.ModelBinding
{
///
/// A context that contains operating information for model binding and validation.
///
public abstract class ModelBindingContext
{
///
/// Represents the associated with this context.
///
///
/// The property setter is provided for unit testing purposes only.
///
public abstract ActionContext ActionContext { get; set; }
///
/// Gets or sets a model name which is explicitly set using an .
///
public abstract string BinderModelName { get; set; }
///
/// Gets or sets a value which represents the associated with the
/// .
///
public abstract BindingSource BindingSource { get; set; }
///
/// Gets or sets the name of the current field being bound.
///
public abstract string FieldName { get; set; }
///
/// Gets the associated with this context.
///
public virtual HttpContext HttpContext => ActionContext?.HttpContext;
///
/// Gets or sets an indication that the current binder is handling the top-level object.
///
/// Passed into the model binding system.
public abstract bool IsTopLevelObject { get; set; }
///
/// Gets or sets the model value for the current operation.
///
///
/// The will typically be set for a binding operation that works
/// against a pre-existing model object to update certain properties.
///
public abstract object Model { get; set; }
///
/// Gets or sets the metadata for the model associated with this context.
///
public abstract 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 abstract string ModelName { get; set; }
///
/// Gets or sets the name of the top-level model. This is not reset to when value
/// providers have no match for that model.
///
public string OriginalModelName { get; protected set; }
///
/// Gets or sets the used to capture values
/// for properties in the object graph of the model when binding.
///
///
/// The property setter is provided for unit testing purposes only.
///
public abstract ModelStateDictionary ModelState { get; set; }
///
/// Gets the type of the model.
///
///
/// The property must be set to access this property.
///
public virtual Type ModelType => ModelMetadata.ModelType;
///
/// Gets or sets a predicate which will be evaluated for each property to determine if the property
/// is eligible for model binding.
///
public abstract Func PropertyFilter { get; set; }
///
/// Gets or sets the . Used for tracking validation state to
/// customize validation behavior for a model object.
///
///
/// The property setter is provided for unit testing purposes only.
///
public abstract ValidationStateDictionary ValidationState { get; set; }
///
/// Gets or sets the associated with this context.
///
public abstract IValueProvider ValueProvider { get; set; }
///
///
/// Gets or sets a which represents the result of the model binding process.
///
///
/// Before an is called, will be set to a value indicating
/// failure. The binder should set to a value created with
/// if model binding succeeded.
///
///
public abstract ModelBindingResult Result { get; set; }
///
/// Pushes a layer of state onto this context. Model binders will call this as part of recursion when binding
/// properties or collection items.
///
///
/// to assign to the property.
///
/// Name to assign to the property.
/// Name to assign to the property.
/// Instance to assign to the property.
///
/// A scope object which should be used in a using statement where
/// is called.
///
public abstract NestedScope EnterNestedScope(
ModelMetadata modelMetadata,
string fieldName,
string modelName,
object model);
///
/// Pushes a layer of state onto this context. Model binders will call this as part of recursion when binding
/// properties or collection items.
///
///
/// A scope object which should be used in a using statement where
/// is called.
///
public abstract NestedScope EnterNestedScope();
///
/// Removes a layer of state pushed by calling .
///
protected abstract void ExitNestedScope();
///
/// Return value of . Should be disposed
/// by caller when child binding context state should be popped off of
/// the .
///
public readonly struct NestedScope : IDisposable
{
private readonly ModelBindingContext _context;
///
/// Initializes the for a .
///
///
public NestedScope(ModelBindingContext context)
{
_context = context;
}
///
/// Exits the created by calling .
///
public void Dispose()
{
_context.ExitNestedScope();
}
}
}
}