比方說,我有一個網頁(建立在MVC 4使用VB.Net),顯示用戶可以通過點擊編輯鏈接編輯的數據。該鏈接將通過傳遞相關數據字段的id
路由至Edit
函數。良好的編程習慣:在另一個函數調用的參數列表中使用函數調用?
要顯示我的編輯頁面,我將抓取數據,將其映射到模型,並將其傳遞給視圖。我們將調用我的映射函數Map2Model
,它接受實體框架模型&返回模型和數據訪問函數GetData
,它返回實體框架模型。 GetData
接受數據的id
,以及userid
。我們將使用SimpleMembership
進行用戶驗證。它是被認爲不好的做法,執行以下操作(調用另一個函數調用的參數列表中的函數:
Function Edit(ByVal id as Integer) as ActionResult
Dim Model2Edit = Map2Model(GetData(id, WebSecurity.GetUserId(User.Identity.Name)))
Return View(Model2Edit)
End Function
VS.(第一聲明變量)
Function Edit(ByVal id as Integer) as ActionResult
Dim TheData as New EntityModel
Dim UserID as Integer
Dim Model2Edit as New Model
UserID = WebSecurity.GetUserId(User.Identity.Name)
TheData = GetData(id, UserID)
Model2Edit = Map2Model(TheData)
Return View(Model2Edit)
End Function
在C#:
public ActionResult Edit(int id)
{
dynamic Model2Edit = Map2Model(GetData(id, WebSecurity.GetUserId(User.Identity.Name)));
return View(Model2Edit);
}
VS.
public ActionResult Edit(int id)
{
EntityModel TheData = new EntityModel();
int UserID = 0;
Model Model2Edit = new Model();
UserID = WebSecurity.GetUserId(User.Identity.Name);
TheData = GetData(id, UserID);
Model2Edit = Map2Model(TheData);
return View(Model2Edit);
}
除了這是否是不好的做法,我希望我的代碼可讀。我覺得應該使描述性的功能應該易於遵循。
編輯修正我的頭銜
EDIT 2進一步細化的問題。
編輯3新增的C#代碼
我不明白你的問題是什麼。你問一個函數調用另一個函數是否是好習慣?如果是的話,答案當然是......你不是一直這麼做嗎? –
是否要求用另一種方法調用方法是不好的做法?當然這不是不好的做法!如何在不調用方法的情況下編程任何東西! – Liam
而不是什麼替代? – Jonesopolis