首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET MVC 範例專案「Suteki.Shop」分析:ModelBinder 基類與派生實作

     編輯:關於ASP.NET

專案建立 DataBinder 基類實作 IModelBinder,派生 ProductBinder、OrderBinder 等,覆蓋絕大多數模型綁定場景。

核心 BindModel 邏輯

public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
  object entity;
  if (declaringAttribute == null || declaringAttribute.Fetch)
    entity = FetchEntity(bindingContext, controllerContext);
  else
    entity = Activator.CreateInstance(bindingContext.ModelType);

  try {
    validatingBinder.UpdateFrom(entity, controllerContext.HttpContext.Request.Form, bindingContext.ModelState, bindingContext.ModelName);
  } catch (ValidationException) { /* 錯誤已記錄於 ModelState */ }
  return entity;
}

FetchEntity 取得既有實體

private object FetchEntity(ModelBindingContext bindingContext, ControllerContext controllerContext) {
  var primaryKey = bindingContext.ModelType.GetPrimaryKey();
  string name = bindingContext.ModelName + "." + primaryKey.Name;
  string rawKeyValue = controllerContext.HttpContext.Request.Form[name];
  if (string.IsNullOrEmpty(rawKeyValue))
    throw new InvalidOperationException("Could not find a value named '{0}'".With(name));
  int key = Convert.ToInt32(rawKeyValue);
  var repository = resolver.GetRepository(bindingContext.ModelType);
  return repository.GetById(key);
}

透過 DataBindAttribute 標記 Action 參數決定是新建還是取得既有實體,配合 BindUsingAttribute 從 IOC 解析具體 Binder,實現聲明式模型綁定。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved