我是否也应该在域模型中使用数据注释? ASP.NETMVC
Should I use data annotations in domain model too? ASP.NET MVC
我是 ASP.NET MVC 的新手,有些事情让我感到困惑。
我正在创建一个 login/registration 网络应用程序,当我来确认密码时,我有点困惑。我当然不想在我的数据库中确认密码列。因此,出于这个原因,我使用了 ViewModel。我在我的 ViewModel 中使用数据注释进行验证。所以不需要在我的域模型中编写任何验证代码。
但是当 Entity Framework 从我的域模型对象创建 table 时,它将从哪里获取信息,例如用户名应包含多少个字符?如果我在我的域模型中使用数据注释,我会写 MaxLength 什么的。
我是否也应该验证域模型中的数据?
您可以在视图模型上使用数据注释来处理客户端验证,并在您的视图中包含 jQuery 验证脚本。
因此在您的视图模型中,您可以像这样设置最小密码长度限制:
using System.ComponentModel.DataAnnotations;
public class RegisterViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
当然,这仅适用于客户端验证,对于服务器端验证,您必须验证控制器中的数据,但我认为您不必在域模型上使用数据注释。
因此在您的控制器中,您可以像这样验证通过的数据
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterViewModel model)
{
//checks for data passed through, if somehow people bypasses client side validation
if (ModelState.IsValid)
{
//continue
}
//validation failed, return to view
return View(model);
}
ModelState.IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. ----
我是 ASP.NET MVC 的新手,有些事情让我感到困惑。 我正在创建一个 login/registration 网络应用程序,当我来确认密码时,我有点困惑。我当然不想在我的数据库中确认密码列。因此,出于这个原因,我使用了 ViewModel。我在我的 ViewModel 中使用数据注释进行验证。所以不需要在我的域模型中编写任何验证代码。
但是当 Entity Framework 从我的域模型对象创建 table 时,它将从哪里获取信息,例如用户名应包含多少个字符?如果我在我的域模型中使用数据注释,我会写 MaxLength 什么的。
我是否也应该验证域模型中的数据?
您可以在视图模型上使用数据注释来处理客户端验证,并在您的视图中包含 jQuery 验证脚本。
因此在您的视图模型中,您可以像这样设置最小密码长度限制:
using System.ComponentModel.DataAnnotations;
public class RegisterViewModel
{
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
当然,这仅适用于客户端验证,对于服务器端验证,您必须验证控制器中的数据,但我认为您不必在域模型上使用数据注释。
因此在您的控制器中,您可以像这样验证通过的数据
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterViewModel model)
{
//checks for data passed through, if somehow people bypasses client side validation
if (ModelState.IsValid)
{
//continue
}
//validation failed, return to view
return View(model);
}
ModelState.IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. ----