ASP.NET MVC 3:不显眼的 JavaScript 验证
有很多关于如何“创建自己的模型”的示例。用数据注释标记它们。 Scott Guthrie 解释如何使用 ORM。我没有发现你的模型何时实际上来自外部 DLL。你如何验证它?
示例:
/* Class coming in from an third-party DLL file. */
public class Person
{
public string Name{get;set;}
public int Age {get;set;}
}
我想到的解决方案:继承外部类,然后将[MetadataType]应用于继承的类。
[Metadata(typeof(Person2_Validation))]
public class Person2:Person{}
public class Person2_Validation
{
[Required,Stringlength(50,ErrorMessage="Name required"]
public string Name{get;set;}
[RegularExpression("([0-9]+)")]
public int Age
}
有更好的办法吗?
There are a lot of examples on how to "create your own model". Mark them with DataAnnotations. Scott Guthrie explains how to validate your model when using an ORM. What I don't find is when your model is actually coming in from an external DLL. How do you validate it?
Example:
/* Class coming in from an third-party DLL file. */
public class Person
{
public string Name{get;set;}
public int Age {get;set;}
}
The solution I am thinking of: Inherit the external class and then apply [MetadataType] to the inherited class.
[Metadata(typeof(Person2_Validation))]
public class Person2:Person{}
public class Person2_Validation
{
[Required,Stringlength(50,ErrorMessage="Name required"]
public string Name{get;set;}
[RegularExpression("([0-9]+)")]
public int Age
}
Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个模型并使用 Mapper(例如 AutoMapper 或 EmitMapper 或 ValueInjecter)在对象之间进行映射,并根据映射模型进行验证。
当您需要将对象传回时,您可以在模型与接收到的模型之间进行映射。
这与 ASP.NET MVC 中的 ViewModel 方法非常相似。
所以它是这样的:
A类(DLL中的类)
B 类(您的模型)
您在 B 上设置所有注释,并创建您需要的任何属性。
您使用的是 B。当您从存储库/源获取某些内容时,您映射(复制所有相关值)A=>B 并发送它(假设作为视图中的模型)。
当您收到返回的 B 时,您会验证它,然后以另一种方式映射它 B=>A,并将其发送到存储库/服务。
顺便说一句:即使模型 A 是您的班级,我也会建议使用这种方法。
为什么在视图中使用 ViewModel 而不是域模型。
You could create a model and use a Mapper (like AutoMapper or EmitMapper or ValueInjecter) to map between your objects, and validate against the mapped model.
When you need to transfer the object back you can map between your model to the recieved model.
This is very similar to a ViewModel approach in ASP.NET MVC.
So it's something like this:
Class A (the class from the DLL)
Class B (your model)
You set all your annotations on B, and create whatever properties you need.
What you use is B. When you get something from the repository/source you map (copy all relevant values) A=>B and send it (let's say as a model in a View).
When you receive B back you validate it, and then map it the other way B=>A, and send it to the repository/service.
BTW: I would recommend using this approach even if model A was YOUR class.
Why use ViewModels instead of Domain Models in Views.
@Linkgoron 答案是正确的。您正在搜索视图模型与域模型。我们需要将 dll 中的模型视为域模型,并将其映射到我们自己的视图模型,就像我们在处理自己的存储库/持久性时所做的那样。这是一个最佳实践。不用担心映射器,它会自动映射。
看这个例子:
http://weblogs.asp.net/shijuvarghese/archive/2010/02/01/view-model-pattern-and-automapper-in-asp-net-mvc-applications.aspx
参见@Nick DeVore 在这里回答了为什么查看模型而不是域模型
为什么有两个类,视图模型和域模型?
最佳实践 - 将视图模型与域模型混合
您的问题可能是以下问题之一原因是:)
@Linkgoron answer is true. You are searching view model vs domain model. we need to think the model from dll is a domain model and map it to our own view model as we did even when dealing with our own repository/persistence. it is a best practice. don't worry about mapper, it will map automatically.
See this example:
http://weblogs.asp.net/shijuvarghese/archive/2010/02/01/view-model-pattern-and-automapper-in-asp-net-mvc-applications.aspx
See this @Nick DeVore answer why view model instead of domain model here
Why Two Classes, View Model and Domain Model?
and also
Bestpractice - Mixing View Model with Domain Model
Your issue could be one of the reason why :)