对我的域对象或视图模型进行验证?它应该去哪里?
我试图让我的服务层摆脱 ASP.NET MVC 依赖项,但我遇到了问题。我想使用 Compare,但它是 asp.net mvc 库的一部分。那我该怎么办呢?
我有一个域类(稍后与 Fluent nhibernate 一起使用)
public class User()
{
[Required(ErrorMessage = "Required")]
[Compare()] // can't do this because not my User domain needs to know about mvc and I rather it not
public virtual string Email (get; set;}
public virtual string ConfirmEmail (get; set;} // not mapped with fluent nhibernate
}
public class UserViewModel()
{
public User User {get; set;}
}
public ActionMethod method()
{
if(!this.ModelState.isValid)
{
}
}
我制作了这个域对象,所以我发现将所有这些属性移动到 UsersViewAModel 中只是为了对它们进行验证,然后将所有这些属性返回到中是毫无意义的将用于提交的域模型(nhibernate commit)
I am trying to keep my service layer free of asp.net mvc dependcies but I have ran into a problem. I want to use Compare but that is part of the asp.net mvc library. So what should I do?
I have a domain class(that is later used with fluent nhibernate)
public class User()
{
[Required(ErrorMessage = "Required")]
[Compare()] // can't do this because not my User domain needs to know about mvc and I rather it not
public virtual string Email (get; set;}
public virtual string ConfirmEmail (get; set;} // not mapped with fluent nhibernate
}
public class UserViewModel()
{
public User User {get; set;}
}
public ActionMethod method()
{
if(!this.ModelState.isValid)
{
}
}
I have this domain object made so I would find it kinda pointless to move all those properties into the UsersViewAModel just to put the validation on them then later on through all those back into the domain model that will be used to commit(nhibernate commit)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能应该为您的模型类使用“伙伴类”来保存验证属性:
验证属性是一项不能在 MVC 关注点之间清晰划分的项目:
它们与视图中的客户端验证相关,与域中的服务器验证相关,在数据存储到数据库并因此需要验证时。
因此,@jfar 说它们应该进入 ViewModel,这与说它们应该进入域的人一样错误。
事实上,恕我直言,将验证属性放在 ViewModel 中是完全不正确的。 ViewModel 应该是发送到视图的数据的容器。它不应该移植自己的任何逻辑。该逻辑(在本例中为验证属性)应在 ViewModel 外部定义,而伙伴类是最佳选择,特别是在您使用自动化 ORM 时,但不仅限于这种特殊情况。
这个问题说明了这样一个事实:并非每个问题都完全映射到 MVC,因此对验证属性应该去哪里的问题采取所有斯大林主义的态度是完全错误的。
我的建议:将它们放在一个地方,与所有其他问题分开。做到这一点的方法是使用伙伴类。
You should probably use a "buddy class" for your model class to hold the validation attributes:
The validation attributes are one item that does not cleanly split between the MVC concerns:
They relate as much to client validation in the views as they do to server validation in the domain, at the point where the data is stored to the database and therefore needs validation.
So @jfar, by saying that they should go in the ViewModel, is as wrong as the person who says they should be in the domain.
In fact, IMHO, it is totally incorrect to put the validation attributes in the ViewModel. The ViewModel should be a container for the data that is sent to the View. It should not port any logic of its own. That logic, in this case Validation Attributes, should be defined outside of the ViewModel and the buddy class is the best option for that, especially if you are using an automated ORM, but not only in that special case.
This question is an illustration of the fact that not every issue maps neatly to MVC, so getting all Stalinistic about where the Validation Attributes should go is plain wrong.
My advice: put them in one place, separate from all other concerns. The way to do that is to use buddy classes.
如果您试图分离关注点,那么将验证属性放在域模型上是最糟糕的事情。
将验证属性放在 ViewModel 上。
Putting validation attributes on your domain models is the worse thing you can do if you are trying to separate concerns.
Put validation attributes on ViewModels.
您可能想使用 AutoMapper 来解决从 User 填充 UserModel 的问题反之亦然。
You might want to use AutoMapper to solve the problem of populating UserModel from User and vice versa.