我有一些關於在ASP.NET MVC 2項目中使用EF4進行良好設計的問題。設計問題:EF4和ASP.NET MVC 2
首先驗證。我是否應該將表單驗證放入實體的SavingChanges事件中?我將如何獲得部分類中的表單數據?還是有更好的方法來做到這一點?
二,維護已登錄的用戶。我來自PHP背景,習慣上在會話中傳遞用戶信息。我應該用一個用戶實體對象來做到這一點嗎?看起來像是過度殺傷。
我有一些關於在ASP.NET MVC 2項目中使用EF4進行良好設計的問題。設計問題:EF4和ASP.NET MVC 2
首先驗證。我是否應該將表單驗證放入實體的SavingChanges事件中?我將如何獲得部分類中的表單數據?還是有更好的方法來做到這一點?
二,維護已登錄的用戶。我來自PHP背景,習慣上在會話中傳遞用戶信息。我應該用一個用戶實體對象來做到這一點嗎?看起來像是過度殺傷。
我寫在ASP.NET MVC 1(.NET 3.5)的應用程序和使用這種 「設計模式」 來處理驗證在我的模型:
namespace MyProject.Models {
public partial class SomeModel {
public bool IsValid {
get { return (GetRuleViolations().Count() == 0); }
}
public IEnumerable<RuleViolation> GetRuleViolations() {
SomeModelRepository smr = new SomeModelRepository();
if (String.IsNullOrEmpty(Title))
yield return new RuleViolation("Title required", "Title");
if (String.IsNullOrEmpty(Author))
yield return new RuleViolation("Author required", "Author");
// Add more validation here if needed
yield break;
}
partial void OnValidate(System.Data.Linq.ChangeAction action)
{
if (!IsValid)
throw new ApplicationException("Rule violations prevent saving");
}
}
}
這依賴於一個名爲RuleViolation類:
namespace MyProject.Models
{
public class RuleViolation
{
public string ErrorMessage { get; private set; }
public string PropertyName { get; private set; }
public RuleViolation(string errorMessage)
{
ErrorMessage = errorMessage;
}
public RuleViolation(string errorMessage, string propertyName)
{
ErrorMessage = errorMessage;
PropertyName = propertyName;
}
}
}
在控制器創建/編輯POST功能,我可以檢查模型來驗證具有:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(SomeModel somemodel)
{
if (ModelState.IsValid)
{
try
{
somemodel.DateCreated = DateTime.Now;
somemodel.DateModified = DateTime.Now;
somemodelRepository.Add(somemodel);
somemodelRepository.Save();
return RedirectToAction("Index");
}
catch
{
foreach (var issue in chapter.GetRuleViolations())
{
// This add the errors to the view so the user knows what went wrong
ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}
}
}
return View(somemodel);
}
然後,您可以使用您的看法以下行來顯示所有的驗證錯誤:
<%= Html.ValidationSummary() %>
而且也把這個旁邊的領域,像這樣:
<label for="Title">Title:</label>
<%= Html.TextBox("Title") %>
<%= Html.ValidationMessage("Title", "*") %>
我不能說這是在MVC 2中做到這一點,但它絕對在MVC 1中爲我工作並繼續在MVC 2中工作。
就用戶會話而言,您可以在ASP.NET MVC中使用它們。我正在使用System.Web.Security.FormsAuthentication和FormsAuthenticationTicket來處理諸如創建會話,保存會話中存儲的用戶名,會話過期時間以及其他一些事情。一旦以此創建會話,您可以根據需要在會話中存儲其他信息。我爲每個頁面加載所需的數據執行此操作。如果它的動態內容像會議期間可能更改的論壇帖子數量一樣,那麼只需根據需要從數據庫中抓取即可。
我發現這段代碼的項目,但它已經永遠,因爲我記得這一切意味着什麼。
FormsAuth.SignIn(userName, rememberMe);
FormsAuthentication.SetAuthCookie(userName, rememberMe);
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddDays(1), rememberMe, Convert.ToString(somePieceOfDataIAlsoWantedSaved));
(我copypasted這個從項目和改變了一些變量名這止跌」不會傷害到仔細檢查我的語法,並檢查相關文檔:p)
這與我使用MVC1和.NET3.5設計的方法完全相同。很高興知道我並不孤單! :) – 2010-10-15 19:46:25
我想我可能從某本書或某個網站上得到了這些,儘管我不記得了。我只知道它可以滿足我的需求,即使它涉及將代碼複製到每個模型中。 – 2010-10-15 19:47:44
哇,好東西!謝謝! – 2010-10-15 20:10:17