我有一些关于在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