环境:

Windows 2008, VS 2008 SP1, Asp.Net Mvc RC1

------------------------------------------------------------------------------

本文通过分析Action Invoking的请求过程片段,探索当前Asp.Net Mvc中的Model Binding是如何实现的。

请求过程片段:

在请求的Action被调用之前,ControllerActionInvoker.InvokeAction()方法被调用,在这个方法里面,有一个ReflectedActionDescriptor的实例会被构建,这个实例包含Action的描述信息。

接着,ControllerActionInvoker.GetParameterValues()方法被调用,通过传入的之前创建的ReflectedActionDescriptor实例,获取Action参数列表(对应的ParameterDescriptor的实例分别被创建),进而遍历各参数,尝试获取参数的值,在遍历的循环里面,ControllerActionInvoker.GetParameterValue()方法被调用。

以下就是ControllerActionInvoker.GetParameterValue()方法的源代码:

Code
protected virtual object GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) {
            
// collect all of the necessary binding properties
            Type parameterType = parameterDescriptor.ParameterType;
            IModelBinder binder 
= GetModelBinder(parameterDescriptor);
            IDictionary
<string, ValueProviderResult> valueProvider = controllerContext.Controller.ValueProvider;
            
string parameterName = parameterDescriptor.BindingInfo.Prefix ?? parameterDescriptor.ParameterName;
            Predicate
<string> propertyFilter = GetPropertyFilter(parameterDescriptor);

            
// finally, call into the binder
            ModelBindingContext bindingContext = new ModelBindingContext() {
                FallbackToEmptyPrefix 
= (parameterDescriptor.BindingInfo.Prefix == null), // only fall back if prefix not specified
                ModelName = parameterName,
                ModelState 
= controllerContext.Controller.ViewData.ModelState,
                ModelType 
= parameterType,
                PropertyFilter 
= propertyFilter,
                ValueProvider 
= valueProvider
            };
            
object result = binder.BindModel(controllerContext, bindingContext);
            
return result;
        }


这个方法做的事情就是,通过ParameterDescriptor的参数描述信息,获取一个与该参数关联的IModelBinder,进而调用IModelBinder.BindModel()方法,得到参数的值。

因此,这里有两个过程:

1) 确定IModelBinder

ControllerActionInvoker.GetModelBinder()被调用,

Code
private IModelBinder GetModelBinder(ParameterDescriptor parameterDescriptor) {
            
// look on the parameter itself, then look in the global table
            return parameterDescriptor.BindingInfo.Binder ?? Binders.GetBinder(parameterDescriptor.ParameterType);
        }


该方法首先尝试从ParameterDescriptor中获取IModelBinder(读取参数的CustomModelBinderAttribute),如果我们没有给参数指定Attribute,则为空,则从Binders中获取,请看这个Binders的定义:

Code
protected internal ModelBinderDictionary Binders {
            
get {
                
if (_binders == null) {
                    _binders 
= ModelBinders.Binders;
                }
                
return _binders;
            }
            
set {
                _binders 
= value;
            }
        }


这里,Binders引用的是ModelBinders.Binders(注意到ModelBinders是个静态类),这时候ModelBinderDictionary要开始发挥作用了,来看ModelBinderDictionary类中的GetBind()方法:

Code
public IModelBinder GetBinder(Type modelType) {
            
return GetBinder(modelType, true /* fallbackToDefault */);
        }

        
public virtual IModelBinder GetBinder(Type modelType, bool fallbackToDefault) {
            
if (modelType == null) {
                
throw new ArgumentNullException("modelType");
            }

            
return GetBinder(modelType, (fallbackToDefault) ? DefaultBinder : null);
        }

        
private IModelBinder GetBinder(Type modelType, IModelBinder fallbackBinder) {
            
// Try to look up a binder for this type. We use this order of precedence:
            
// 1. Binder registered in the global table
            
// 2. Binder attribute defined on the type
            
// 3. Supplied fallback binder

            IModelBinder binder;
            
if (_innerDictionary.TryGetValue(modelType, out binder)) {
                
return binder;
            }

            binder 
= ModelBinders.GetBinderFromAttributes(modelType,
                () 
=> String.Format(CultureInfo.CurrentUICulture, MvcResources.ModelBinderDictionary_MultipleAttributes, modelType.FullName));

            
return binder ?? fallbackBinder;
        }


这是该方法的3个重载,按顺序被调用,当第三个重载方法被调用时,它首先会尝试从ModelBinderDictionary的_innerDictionary变量中取modelType对应的IModelBinder(前面提到ModelBinders是个静态类,因此我们就有机会添加类型和自定义IModelBinder的映射到这个字典里面),否则再次尝试从Attribute中获取,如果都找不到,那么就返回DefaultBinder,这里DefaultBinder是DefaultModelBinder的一个实例。最终,与参数对应的IModelBinder被宣告确定,接下来就是通过IModelBinder.BindMode()方法来给参数赋值了。

2) 取值赋值

接下来的事情,就是对应的IModelBinder发挥作用,结合controllerContext.Controller.ValueProvider来取值,并对参数赋值。

....

然后被请求的Action被正式Executed。

总结:

本文仅仅分析了一个很小的片段,关于更多相关的东西,将在以后的文章中继续探索。

以下列出相关的几个类,大家有兴趣可以看看:

CustomModelBinderAttribute, ModelBinderAttribute, BindAttribute, IModelBinder, DefaultModelBinder, ModelBinderDictionary, ModelBinders, ModelBindingContext, 以及几个xxxDescriptor。

posted on 2009-02-23 21:02  Tristan G  阅读(3604)  评论(5编辑  收藏  举报