如何向我的 POCO(模板)类添加验证

发布于 2024-10-19 06:24:46 字数 234 浏览 2 评论 0原文

所以我使用 这个教程生成我的 poco 类,我将在整个应用程序中使用它。问题是我不应该修改生成的 cs 文件,因为它们会自动重新生成...我如何添加像 [必需]之类的东西?请帮忙

So I used this tutorial to generate my poco classes which I am to use throughout my aplication.. the problem is that Im not supposed to modify the generated cs files cause they get autoregenerated... How do I add attributes like [Required] and stuff like that?? please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

雪化雨蝶 2024-10-26 06:24:46

您不能直接添加它(除非您修改 T4 模板来为您创建它们),但您可以尝试使用 ASP.NET 动态数据中引入的技巧。所有 POCO 类都定义为部分类。因此,让我们定义您的部分:

using System.ComponentModel.DataAnnotations;

[MetadataType(typeof(MyClassMetadata))]
public partial class MyClass
{
  private class MyClassMetadata
  {
     [Required]
     public object Id;

     [Required]
     [StringLength(100)]
     public object Name;
  }
}

元数据类是仅保存元数据的特殊类型 - 它从未被使用。字段名称必须与真实类中相应字段相同(字段类型并不重要,因此您可以使用object)。

无论如何,在 ASP.NET MVC 中,您应该为每个视图使用专门的视图模型并传递所需的数据,以便验证属性将放置在视图模型类中。

You can't add it directly (unless you modify T4 template to create them for you) but you can try to use trick introduced in ASP.NET dynamic data. All POCO classes are defined as partial. So lets define your partial part:

using System.ComponentModel.DataAnnotations;

[MetadataType(typeof(MyClassMetadata))]
public partial class MyClass
{
  private class MyClassMetadata
  {
     [Required]
     public object Id;

     [Required]
     [StringLength(100)]
     public object Name;
  }
}

Metadata class is special type to hold only metadata - it is never used. Name of fields must be same as corresponding fields in real class (field types doesn't matter so you can use object).

Anyway in ASP.NET MVC you should use specialized View model for each view and pass data you need so the validation attributes will be placed in view model class.

唐婉 2024-10-26 06:24:46

生成的 POCO 上的属性源自模型中实体的方面。例如,对于 [Required] 确保字段为“not null”,对于 [StringLength(n)] 确保数据类型为 nvarchar(n) 通过 MaxLength 方面。

The attributes on the generated POCOs are derived from the facets on the entities in the model. e.g. for [Required] make sure the field is "not null" and for [StringLength(n)] make sure the datatype is nvarchar(n) via the MaxLength facet.

小霸王臭丫头 2024-10-26 06:24:46

进一步扩展答案。通过使用 Microsoft Patterns &通过实践企业库 5 验证块,您可以开辟除普通数据注释之外的丰富验证可能性。

using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

[HasSelfValidation]
public partial class Category : ICategory
{
    [SelfValidation]
    public void Validate(ValidationResults validationResults)
    {
        if (this.Title === "Credo")
        {
            validationResults.AddResult(
                new ValidationResult(
                    "Category title cannot be a veiled reference to a former cool 2000AD character.",
                    this,
                    null,
                    null,
                    null));
        }

        validationResults.AddAllResults(
            ValidationFactory
            .CreateValidator<ICategory>()
            .Validate(this));
    }
}

using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

public interface ICategory
{
    int Id
    {
        get; 
        set;
    }

    [Required]
    [StringLengthValidator(1, 50, MessageTemplate = "Category title should be a maximum of 50 characters in length.")]
    string Title
    {
        get; 
        set;
    }
}

Further expanding on the answer. By using Microsoft Patterns & Practices Enterprise Library 5 Validation Block, you can open up a wealth of validation possibilities beyond those available through normal data annotations.

using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

[HasSelfValidation]
public partial class Category : ICategory
{
    [SelfValidation]
    public void Validate(ValidationResults validationResults)
    {
        if (this.Title === "Credo")
        {
            validationResults.AddResult(
                new ValidationResult(
                    "Category title cannot be a veiled reference to a former cool 2000AD character.",
                    this,
                    null,
                    null,
                    null));
        }

        validationResults.AddAllResults(
            ValidationFactory
            .CreateValidator<ICategory>()
            .Validate(this));
    }
}

using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

public interface ICategory
{
    int Id
    {
        get; 
        set;
    }

    [Required]
    [StringLengthValidator(1, 50, MessageTemplate = "Category title should be a maximum of 50 characters in length.")]
    string Title
    {
        get; 
        set;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文