POCO - 如果 POCO 意味着仅具有属性的纯 .net 类,我可以在 MVC 中编写验证

发布于 2024-12-25 21:11:03 字数 2022 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人间☆小暴躁 2025-01-01 21:11:03

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.

她比我温柔 2025-01-01 21:11:03

链接的文章并没有说 POCO 一定不能有方法。 POCO 是什么的清晰描述可以在 Wikipedia 上找到:

...该术语用于将一个简单的对象与一个简单的对象进行对比
设计用于与复杂的、特殊的对象框架一起使用,例如
作为 ORM 组件。另一种说法是 POCO 是对象
不受特定所需的继承或属性的阻碍
框架。

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:

... the term is used to contrast a simple object with one that is
designed to be used with a complicated, special object frameworks such
as an ORM component. Another way to put it is that POCOs are objects
unencumbered with inheritance or attributes needed for specific
frameworks.

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.

任谁 2025-01-01 21:11:03

就我个人而言,我喜欢使用定义该模型所需的基本属性来创建 POCO 部分类,然后将验证逻辑放在单独的类中。例如:

public partial class Wizard
{
    public string UserName { get; set; }
    public string EmailAddress { get; set; }
}

然后如果我想向 UserName 添加验证:

public partial class Wizard
{
    [Required]
    [StringLength(20)]
    public string UserName { get; set; }
}

我知道编译器无论如何都会合并这两个类,并且您可能会重复属性,但我认为这是最干净的方法。

另一种选择是使用 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:

public partial class Wizard
{
    public string UserName { get; set; }
    public string EmailAddress { get; set; }
}

and then if I wanted to add validation to UserName:

public partial class Wizard
{
    [Required]
    [StringLength(20)]
    public string UserName { get; set; }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文