Model 和 ViewModel 的验证最佳实践
我有单独的模型和视图模型类。其中 viewmodel 类仅进行 UI 级别验证(请参阅:验证:模型或 ViewModel )。
我可以在控制器中的后期操作中验证模型(vewmodel)是否有效。
询问: 如何验证模型(带有数据注释的主要实体)。
我没有使用模型对象开发视图模型。只需复制属性并添加该特定视图中可能需要的所有属性。
//Model Class
public class User
{
[Required]
public string Email {get; set;}
[Required]
public DateTime Created {get; set;}
}
//ViewModel Class
public class UserViewModel
{
[Required]
public string Email {get; set;}
[Required]
public string LivesIn {get; set;}
}
//Post action
public ActionResult(UserViewModel uvm)
{
if( ModelState.IsValid)
//means user entered data correctly and is validated
User u = new User() {Email = uvm.Email, Created = DateTime.Now};
//How do I validate "u"?
return View();
}
应该做这样的事情:
var results = new List<ValidationResult>();
var context = new ValidationContext(u, null, null);
var r = Validator.TryValidateObject(u, context, results);
我想在(业务实体的)基类中添加此验证技术,并在从视图模型类映射到业务实体时验证它。
有什么建议吗?
I have separate model and viewmodel classes. Where viewmodel classes only do UI level validation (refer: Validation: Model or ViewModel).
I can verify on post action in a controller that model (vewmodel) is valid.
The ask:
How do I validate the model (the main entity with data annotations).
I am not developing the viewmodel using model object. Just duplicating the properties and adding all properties possibly required in that particular view.
//Model Class
public class User
{
[Required]
public string Email {get; set;}
[Required]
public DateTime Created {get; set;}
}
//ViewModel Class
public class UserViewModel
{
[Required]
public string Email {get; set;}
[Required]
public string LivesIn {get; set;}
}
//Post action
public ActionResult(UserViewModel uvm)
{
if( ModelState.IsValid)
//means user entered data correctly and is validated
User u = new User() {Email = uvm.Email, Created = DateTime.Now};
//How do I validate "u"?
return View();
}
Should do something like this:
var results = new List<ValidationResult>();
var context = new ValidationContext(u, null, null);
var r = Validator.TryValidateObject(u, context, results);
What I am thinking is adding this validation technique in the base class (of business entity), and verify it when I am mapping from viewmodel class to business entity.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1)对从用户检索信息的模型使用流畅的验证。它比数据注释更灵活,更容易测试。
2)您可能想研究一下automapper,通过使用automapper,您不必编写
x.name = y.name
。3)对于您的数据库模型,我会坚持使用数据注释。
以下所有内容均基于新信息
首先,您应该在两个位置上进行验证,就像您现在对实际模型验证所做的那样,这就是我的做法。 免责声明:这不是完美的方法
首先将
UserViewModel
更新为然后将操作方法更新为
Now 对于用户模型来说,这会变得很棘手,并且您有许多可能的解决方案。我想出的解决方案(可能不是最好的)如下:
最后一些单元测试来检查我自己的代码:
1) Use fluent validation on the model that the retrieves information from the user. it is more flexible then data annotation and easier to test.
2) You might want to look into automapper, by using automapper you don't have to write
x.name = y.name
.3) For your database model I would stick to the data-annotations.
Everything below is based on the new information
First and all you should place validation on both location like you did now for the actual model validation this is how I would do it. Disclaimer: this is not the perfect way
First and all update the
UserViewModel
toThen update the action method to
Now for the user model it gets tricky and you have many possible solutions. The solution I came up with (probably not the best) is the following:
Last some unit test to check my own code:
我认为使用数据注释更好
看看这个
ASP .NET MVC Valdation
对于服务器端验证,您可以使用 Fluent验证
并查看此问题
I think is better to use Data Annotations
look at this
ASP.NET MVC Valdation
and for server side validation you can use the fluent validation
and look at this question