POCO - 如果 POCO 意味着仅具有属性的纯 .net 类,我可以在 MVC 中编写验证
POCO 很新,找到一些谷歌链接,但发现了很多不同的故事。 有些与实体框架、延迟加载等相关。有些说它是纯 .det 类。 至少MSDN。
来自 MSDN 的定义 POCO 的链接: msdn.microsoft.com/en-us/library/dd456872.aspx
我相信MSDN(一个简单的定义),并假设它是一个纯.NET类。
现在让我进入正题。 如果它是纯 .net 类,内部只有属性,那么它就相当于 MVC 中的“MODEL”。 例子。
[Required(ErrorMessage = "Full Name required.")]
[StringLength(20, ErrorMessage = "Username must be under 20 chars.")]
public string UserName { get; set; }
[Required(ErrorMessage = "Email required.")]
[RegularExpression(".+@.+\\..+", ErrorMessage = "Email not valid.")]
public string Email { get; set; }
[Required(ErrorMessage = "PassWord required.")]
[StringLength(20, ErrorMessage = "Maximum 20 chars. allow")]
[DataType(DataType.Password)]
public string Password { get; set; }
到了这个水平我就很清楚了。现在,如果我想在 MODEL 中编写自己的验证(条件) 使用
ValidationAttribute 或
IValidatableObject
如果我没记错的话,这将不是纯 .net 类。 例如...(如下所示)
public class Wizard : ValidationAttribute,IValidatableObject
{
public override bool IsValid(object value)
{
return base.IsValid(value);
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
throw new NotImplementedException();
}
[Required(ErrorMessage = "Full Name required.")]
[StringLength(20, ErrorMessage = "Username must be under 20 chars.")]
public string UserName { get; set; }
[Required(ErrorMessage = "Email required.")]
[RegularExpression(".+@.+\\..+", ErrorMessage = "Email not valid.")]
public string Email { get; set; }
[Required(ErrorMessage = "PassWord required.")]
[StringLength(20, ErrorMessage = "Maximum 20 chars. allow")]
[DataType(DataType.Password)]
public string Password { get; set; }
}
这还是 POCO 吗? 如果是,它如何包含方法。(与 MSDN 链接相反) 如果否,我应该在哪里写下我的验证代码(当然是 MVC 中的条件验证)。 寻找一个非常好的答案和一个例子。
Very new to POCO, find some google links but found many different stories.
Some connected with Entity framework, lazy loading etc. Some says its a pure .det class.
Atleast MSDN.
LINK FOR DEFINE POCO FROM MSDN:
msdn.microsoft.com/en-us/library/dd456872.aspx
I trust MSDN(a simple defination), and assume that it is a pure .NET Class.
Now let me come to the point.
IF it is pure .net class with only properties inside it than it is equilateral to "MODEL" in MVC.
example.
[Required(ErrorMessage = "Full Name required.")]
[StringLength(20, ErrorMessage = "Username must be under 20 chars.")]
public string UserName { get; set; }
[Required(ErrorMessage = "Email required.")]
[RegularExpression(".+@.+\\..+", ErrorMessage = "Email not valid.")]
public string Email { get; set; }
[Required(ErrorMessage = "PassWord required.")]
[StringLength(20, ErrorMessage = "Maximum 20 chars. allow")]
[DataType(DataType.Password)]
public string Password { get; set; }
Upto this level it is clear to me. Now if i want to write my own validation (conditional) in MODEL
using
ValidationAttribute
or
IValidatableObject
this will be not pure .net class if i am not wrong.
example.... (Something like below)
public class Wizard : ValidationAttribute,IValidatableObject
{
public override bool IsValid(object value)
{
return base.IsValid(value);
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
throw new NotImplementedException();
}
[Required(ErrorMessage = "Full Name required.")]
[StringLength(20, ErrorMessage = "Username must be under 20 chars.")]
public string UserName { get; set; }
[Required(ErrorMessage = "Email required.")]
[RegularExpression(".+@.+\\..+", ErrorMessage = "Email not valid.")]
public string Email { get; set; }
[Required(ErrorMessage = "PassWord required.")]
[StringLength(20, ErrorMessage = "Maximum 20 chars. allow")]
[DataType(DataType.Password)]
public string Password { get; set; }
}
Is this the POCO still?
If yes, how can it contains methods.(opposite to MSDN link)
IF NO, where should i write down my validation code (of course conditional validation in MVC).
Looking for a really great answer with an example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
POCO 意味着您不必继承某些框架定义的基类即可实现功能。基本上你可以自由地设计你的类层次结构。
您可以添加自己的方法,无论是验证还是一些业务逻辑。
一个反例是继承 EntityObject 实体框架中实体的类。
POCOs mean you do not have to inherit from some framework defined base class in order to implement functionality. Basically you are free to design your class hierarchy.
You can add your own methods be it validation or some business logic.
A counter example would be to inherit from EntityObject class for entities in Entity Framework.
链接的文章并没有说 POCO 一定不能有方法。 POCO 是什么的清晰描述可以在 Wikipedia 上找到:
POCO 可以拥有您需要的所有方法或逻辑。 POCO 和非 POCO 之间的区别在于,POCO 是即使您不使用特定框架(EF)也可以始终使用的类,但非 POCO 仅当链接特定框架或更糟糕的初始化和使用时才可以使用。
对于纯粹主义者来说,数据注释也违反了 POCO,因为它们也需要特定的 API,但在务实的方法中,数据注释是可以的(除了 EF Code First 中使用的特殊注释来定义映射),因为它们仅带来对实体验证方式的依赖,而不是对实体的验证方式的依赖。依赖于实体的持久化方式(非 POCO EF 对象)。对持久性的依赖可能会要求在您从未期望使用 EF 的程序集中引用 EF - 例如 MVC 应用程序之类的表示层。
The linked article doesn't say that POCO mustn't have methods. Clear description what POCO is can be found on Wikipedia:
POCO can have all methods or logic you need. The difference between POCO and non-POCO is that POCO is class you can use always even if you don't use specific framework (EF) but non-POCO can be used only when specific framework is linked or even worse initialized and used.
For purists data annotations violates POCO as well because they also demand specific API but in pragmatic approach data annotations are OK (except special annotations used in EF Code first to define mapping) because they bring dependency only to the way how entity is validated but not the dependency to the way how entity is persisted (non-POCO EF object). Dependency on persistence can demand reference to EF in assemblies where you never expect to use EF - for example presentation layer like MVC application.
就我个人而言,我喜欢使用定义该模型所需的基本属性来创建 POCO 部分类,然后将验证逻辑放在单独的类中。例如:
然后如果我想向
UserName
添加验证:我知道编译器无论如何都会合并这两个类,并且您可能会重复属性,但我认为这是最干净的方法。
另一种选择是使用 MetadataType 属性。
Personally I like to make my POCOs partial classes with the basic properties needed to define that model and then put and validation logic in a separate class. e.g:
and then if I wanted to add validation to
UserName
:I know the complier just amalgamates the two classes anyway and you may be repeating properties but I think its the cleanest approach.
Another option is to use the MetadataType attribute.