元数据类型问题

发布于 2024-08-14 22:18:18 字数 1556 浏览 7 评论 0原文

我正在使用 VS2008 SP1、WCF Ria 服务 2009 年 7 月 CTP。我发现 MetadataType 在部分类模式下不起作用,真的不知道我错过了什么:

工作:-

public partial class Person
{
    private string _Name;

    [Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")]
    [StringLength(3)]
    public string Name
    {
        set{_Name = value;}
        get{return _Name;}
    }
}

class Program
{
    static void Main(string[] args)
    {
        Person p = new Person { Name="123432" };
        List res = new List();
        Validator.TryValidateObject(p,new ValidationContext(p,null,null),
            res,true);
        if (res.Count > 0)
        {
            Console.WriteLine(res[0].ErrorMessage);
            Console.ReadLine();
        }
    }
}

不工作

public partial class Person
{
    private string _Name;

    public string Name
    {
        set{_Name = value;}
        get{return _Name;}
    }
}

[MetadataType(typeof(PersonMetadata))]
public partial class Person
{
}


public partial class PersonMetadata
{
    [Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")]
    [StringLength(3)]
    public string Name;
}

class Program
{
    static void Main(string[] args)
    {
        Person p = new Person { Name="123432" };
        List res = new List();
        Validator.TryValidateObject(p,new ValidationContext(p,null,null),
            res,true);
        if (res.Count > 0)
        {
            Console.WriteLine(res[0].ErrorMessage);
            Console.ReadLine();
        }
    }
}

I'm using VS2008 SP1, WCF Ria Service July 2009 CTP. I found out that MetadataType does not work in partial class mode, really don't know what I have missed out:

Work:-

public partial class Person
{
    private string _Name;

    [Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")]
    [StringLength(3)]
    public string Name
    {
        set{_Name = value;}
        get{return _Name;}
    }
}

class Program
{
    static void Main(string[] args)
    {
        Person p = new Person { Name="123432" };
        List res = new List();
        Validator.TryValidateObject(p,new ValidationContext(p,null,null),
            res,true);
        if (res.Count > 0)
        {
            Console.WriteLine(res[0].ErrorMessage);
            Console.ReadLine();
        }
    }
}

Not Work

public partial class Person
{
    private string _Name;

    public string Name
    {
        set{_Name = value;}
        get{return _Name;}
    }
}

[MetadataType(typeof(PersonMetadata))]
public partial class Person
{
}


public partial class PersonMetadata
{
    [Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")]
    [StringLength(3)]
    public string Name;
}

class Program
{
    static void Main(string[] args)
    {
        Person p = new Person { Name="123432" };
        List res = new List();
        Validator.TryValidateObject(p,new ValidationContext(p,null,null),
            res,true);
        if (res.Count > 0)
        {
            Console.WriteLine(res[0].ErrorMessage);
            Console.ReadLine();
        }
    }
}

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

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

发布评论

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

评论(3

脱离于你 2024-08-21 22:18:18

编辑:我在这里找到了答案:http:// /forums.silverlight.net/forums/p/149264/377212.aspx

在验证之前,您需要手动注册元数据类:

TypeDescriptor.AddProviderTransparent(
            new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Person), typeof(PersonMetadata)), typeof(Person));

        List<ValidationResult> res = new List<ValidationResult>();
        bool valid = Validator.TryValidateObject(p, new ValidationContext(p, null, null), res, true);

(原始答案如下)

问题并不具体对于您的部分类, Validator.TryValidateObject 似乎无法识别 MetaDataType 属性。我有同样的问题 - MVC 2 中的内置验证可以识别元数据类,但 TryValidateObject 不能。

看看这些:
使用 Validator 类验证 DataAnnotations
当我使用 Validator.TryValidateObject 时验证不起作用

作为旁注,我不知道是否有必要,但我见过的元数据类的所有示例都在每个属性上使用默认的获取/设置:

[Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")]
[StringLength(3)]
public string Name { get; set; }

EDIT: I found the answer here: http://forums.silverlight.net/forums/p/149264/377212.aspx

Before validating, you need to manually register the metadata class:

TypeDescriptor.AddProviderTransparent(
            new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Person), typeof(PersonMetadata)), typeof(Person));

        List<ValidationResult> res = new List<ValidationResult>();
        bool valid = Validator.TryValidateObject(p, new ValidationContext(p, null, null), res, true);

(Original answer follows)

The problem isn't specifically with your partial class, it's that Validator.TryValidateObject doesn't seem to recognize the MetaDataType attribute. I have the same problem - the built-in validation in MVC 2 recognizes the metadata class, but TryValidateObject doesn't.

See these:
Validating DataAnnotations with Validator class
Validation does not work when I use Validator.TryValidateObject

As a side note, I don't know if it's necessary, but all examples I've seen for metadata classes employ the default get/set on each property:

[Required(AllowEmptyStrings=false, ErrorMessage="Name required entry")]
[StringLength(3)]
public string Name { get; set; }
自此以后,行同陌路 2024-08-21 22:18:18

非常感谢杰里米·格伦瓦尔德(Jeremy Gruenwald)的上述回答……我完全被困在这个问题上了。

我想基于此解决方案创建一个标准验证类,但我不想必须传递元数据类类型,因为它感觉很丑。

为了实现这一目标,我创建了一个静态类,它对自定义属性进行查找以获取元数据类类型,然后在返回验证结果之前注册该类。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace MyApp.Validation
{
    public static class EntityValidator
    {
        public static List<ValidationResult> Validate(object instance, bool validateAllProperties = true)
        {
            RegisterMetadataClass(instance);

            var validationContext = new ValidationContext(instance, null, null);
            var validationResults = new List<ValidationResult>();

            Validator.TryValidateObject(instance, validationContext, validationResults, validateAllProperties);

            return validationResults;
        }

        private static void RegisterMetadataClass(object instance)
        {
            var modelType = instance.GetType();
            var metadataType = GetMetadataType(modelType);

            if (metadataType != null) 
            {
                TypeDescriptor.AddProviderTransparent(new AssociatedMetadataTypeTypeDescriptionProvider(modelType, metadataType), modelType);
            }
        }

        private static Type GetMetadataType(Type type)
        {
            var attribute = (MetadataTypeAttribute)type.GetCustomAttributes(typeof (MetadataTypeAttribute), true).FirstOrDefault();
            return attribute == null ? null : attribute.MetadataClassType;
        }
    }
}

用法很简单:

var errors = EntityValidator.Validate(myEntity);

Many thanks to Jeremy Gruenwald for the answer above... I was completely stuck on this one.

I wanted to create a standard validation class based on this solution, but I didn't want to have to pass in the metadata class type because it just felt ugly.

To achieve this I created a static class which does a lookup on the custom attributes to get the metadata class type and then registers that class before returning the validation results.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace MyApp.Validation
{
    public static class EntityValidator
    {
        public static List<ValidationResult> Validate(object instance, bool validateAllProperties = true)
        {
            RegisterMetadataClass(instance);

            var validationContext = new ValidationContext(instance, null, null);
            var validationResults = new List<ValidationResult>();

            Validator.TryValidateObject(instance, validationContext, validationResults, validateAllProperties);

            return validationResults;
        }

        private static void RegisterMetadataClass(object instance)
        {
            var modelType = instance.GetType();
            var metadataType = GetMetadataType(modelType);

            if (metadataType != null) 
            {
                TypeDescriptor.AddProviderTransparent(new AssociatedMetadataTypeTypeDescriptionProvider(modelType, metadataType), modelType);
            }
        }

        private static Type GetMetadataType(Type type)
        {
            var attribute = (MetadataTypeAttribute)type.GetCustomAttributes(typeof (MetadataTypeAttribute), true).FirstOrDefault();
            return attribute == null ? null : attribute.MetadataClassType;
        }
    }
}

usage is a simple as:

var errors = EntityValidator.Validate(myEntity);
孤独患者 2024-08-21 22:18:18

如果您正在使用 WPF 和 EF,这对我来说一直有效...

[MetadataType(typeof(Department.Metadata))]
public partial class Department : BaseModel
{
    static Department()
    {
        TypeDescriptor.AddProvider(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Department),typeof(Metadata)), typeof(Department));
    }
    private sealed class Metadata
    {
        [Required(AllowEmptyStrings = false, ErrorMessage = "Department Name is required.")]
        [StringLength(50, ErrorMessage = "Name must be between 3 and 50 characters.", MinimumLength = 3)]
        public string Name;

        [StringLength(250, ErrorMessage = "Name must be between 10 and 250 characters.", MinimumLength = 10)]
        public string Description;
    }
}

以及实现它的基类...

public abstract class BaseModel : IDataErrorInfo
{
    #region Validation
    string IDataErrorInfo.Error
    {
        get { return null; }
    }
    string IDataErrorInfo.this[string propertyName]
    {
        get
        {
            var propertyInfo = GetType().GetProperty(propertyName);
            var results = new List<ValidationResult>();
            var result = Validator.TryValidateProperty(propertyInfo.GetValue(this, null), new ValidationContext(this, null, null)
            {
                MemberName = propertyName
            }, results);

            if (result) return string.Empty;
            var validationResult = results.First();
            return validationResult.ErrorMessage;
        }
    }
    #endregion
}

If you are working with WPF and EF, this has always worked for me...

[MetadataType(typeof(Department.Metadata))]
public partial class Department : BaseModel
{
    static Department()
    {
        TypeDescriptor.AddProvider(new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Department),typeof(Metadata)), typeof(Department));
    }
    private sealed class Metadata
    {
        [Required(AllowEmptyStrings = false, ErrorMessage = "Department Name is required.")]
        [StringLength(50, ErrorMessage = "Name must be between 3 and 50 characters.", MinimumLength = 3)]
        public string Name;

        [StringLength(250, ErrorMessage = "Name must be between 10 and 250 characters.", MinimumLength = 10)]
        public string Description;
    }
}

And the base class that makes it happen...

public abstract class BaseModel : IDataErrorInfo
{
    #region Validation
    string IDataErrorInfo.Error
    {
        get { return null; }
    }
    string IDataErrorInfo.this[string propertyName]
    {
        get
        {
            var propertyInfo = GetType().GetProperty(propertyName);
            var results = new List<ValidationResult>();
            var result = Validator.TryValidateProperty(propertyInfo.GetValue(this, null), new ValidationContext(this, null, null)
            {
                MemberName = propertyName
            }, results);

            if (result) return string.Empty;
            var validationResult = results.First();
            return validationResult.ErrorMessage;
        }
    }
    #endregion
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文