MVC中的默认Model绑定者DefaultModelBinder

一、题外话

接续上一篇:

MVC中Action参数绑定的过程

 public IModelBinder DefaultBinder
{
    get
    {
        if (this._defaultBinder == null)
        {
            this._defaultBinder = new DefaultModelBinder();
        }
        return this._defaultBinder;
    }
    set
    {
        this._defaultBinder = value;
    }
}

二、分析DefaultModelBinder

public class DefaultModelBinder : IModelBinder
{
    public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext){}//其他方法我们暂时不分心,不止这一个方法
}
public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
            EnsureStackHelper.EnsureStack();
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            bool flag = false;
            if (!string.IsNullOrEmpty(bindingContext.ModelName) && !bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
            {//ValueProvider是一个IValueProvider的实例,我们下面分析一下这个东西
                if (!bindingContext.FallbackToEmptyPrefix)
                {
                    return null;
                }
                bindingContext = new ModelBindingContext
                {
                    ModelMetadata = bindingContext.ModelMetadata,
                    ModelState = bindingContext.ModelState,
                    PropertyFilter = bindingContext.PropertyFilter,
                    ValueProvider = bindingContext.ValueProvider
                };
                flag = true;
            }
            if (!flag)
            {
                bool flag2 = DefaultModelBinder.ShouldPerformRequestValidation(controllerContext, bindingContext);
                IUnvalidatedValueProvider arg_9B_0 = bindingContext.UnvalidatedValueProvider;
                bool skipValidation = !flag2;
                ValueProviderResult value = arg_9B_0.GetValue(bindingContext.ModelName, skipValidation);
                if (value != null)
                {
                    return this.BindSimpleModel(controllerContext, bindingContext, value);
                }
            }
            if (!bindingContext.ModelMetadata.IsComplexType)
            {
                return null;
            }
            return this.BindComplexModel(controllerContext, bindingContext);
}

看看IValueProvider

    public interface IValueProvider
    {
        /// Determines whether the collection contains the specified prefix.
        /// true if the collection contains the specified prefix; otherwise, false.
        /// The prefix to search for.
        bool ContainsPrefix(string prefix);
        /// Retrieves a value object using the specified key.
        /// The value object for the specified key.
        /// The key of the value object to retrieve.
        ValueProviderResult GetValue(string key);
    }

可以看看这篇博文:http://www.cnblogs.com/artech/archive/2012/05/17/value-provider-01.html

 

 

你可能感兴趣的:(MVC中的默认Model绑定者DefaultModelBinder)