被分配到子對象,我有兩個目標: -我怎麼能防止編輯父對象,如果它已經在我的asp.net MVC Web應用程序
LabTest
LabTestDetails
凡LabTest
對象可以有零個或多個LabTestDetails
對象。 我需要實現以下業務規則: -
如果LabTest對象已分配給一個或多個LabTestDetails對象,則用戶應該無法編輯LabTest對象。在
public partial class LabTest
{
public bool IsAlreadyAssigned(int id)
{
return (LabTestDetailss.Any(r2 => r2.LabTestID == id));
}}
然後,我添加了以下檢查 - :
目前,我已經實現了LABTEST對象命名IsAlreadyAssigned
一個輔助方法(以檢查LABTEST對象已被分配給任何LabTestDetails對象)在Get & Post
編輯操作方法: -
public ActionResult Edit(int id)
{
LabTest c = repository.GetLabTest (id);
if ((c == null) || (c.IsAlreadyAssigned (id)))
{
return View("Error");
}
return View(c);
}
[HttpPost]
public ActionResult Edit(int id, FormCollection colletion)
{
LabTest c = repository.GetLabTest (id);
if ((c == null) || (c.IsAlreadyAssigned (id))) // *******
{
return View("Error");
}
try
{
if (TryUpdateModel(c))
{
elearningrepository.Save();
return RedirectToAction("Details", new { id = c.LabTestID });
}
}
以上可能正常工作在大多數的情況下,但如果LabTest
對象只是指定給labTestDetails OBJ在if ((c == null) || (c.IsAlreadyAssigned (id)))
檢查後的行動方法後,我將其標記爲(*)上的代碼,然後我的業務邏輯將被打破。
所以有沒有一種方法來實現我的動作方法,以便它將始終防止編輯LabTest對象,如果它已被分配給LabTestdetail對象。
BR
如果您在幕後使用Linq to Sql或Entity Framework,那麼您可能可以通過併發檢查來完成此操作 – 2012-04-23 22:03:38
我正在使用實體框架,但它不處理這種情況;因爲這種商業規則沒有實施,並且不能在數據庫級別實施......所以EF將無法檢測到這種安全事件。 – 2012-04-24 00:13:21
你是說它不能在數據庫級別實現?你打算使用存儲的特效? – MisterJames 2012-04-24 04:07:46