環境:
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。