C# } /// ///验证实体的单个属性。 /// ///包含已验证属性的实体类型。 ///包含已验证属性的实体。 ///正在验证的属性的名称。 ///实体不包含具有提供名称的属性。 ///属性的值无效。 公共静态void ValidateProperty(tenty实体,字符串成员名),其中tenty:class { 类型entityType=entity.GetType(); PropertyInfo property=entityType.GetProperty(memberName); if(属性==null) { 抛出新的InvalidOperationException(String.Format(CultureInfo.InvariantCulture, “实体不包含名为{0}”(memberName))的属性; } AssociateMetadataType(实体); var value=property.GetValue(实体,空); var context=新的ValidationContext(实体,null,null); context.MemberName=MemberName; ValidateProperty(值、上下文); } // http://buildstarted.com/2010/09/16/metadatatypeattribute-with-dataannotations-and-unit-testing/ //MetadataTypeAttribute定义的数据批注不会自动包括在内。必须注入这些定义。 私有静态void AssociateMetadataType(对象实体) { var entityType=entity.GetType(); foreach(entityType.GetCustomAttributes(typeof(MetadataTypeAttribute),true.Cast()中的var属性) { TypeDescriptor.AddProviderTransparent( 新的关联MetadataTypeDescriptionProvider(entityType,attribute.MetadataClassType),entityType); } } }
此验证器的最大缺点是:C# } /// ///验证实体的单个属性。 /// ///包含已验证属性的实体类型。 ///包含已验证属性的实体。 ///正在验证的属性的名称。 ///实体不包含具有提供名称的属性。 ///属性的值无效。 公共静态void ValidateProperty(tenty实体,字符串成员名),其中tenty:class { 类型entityType=entity.GetType(); PropertyInfo property=entityType.GetProperty(memberName); if(属性==null) { 抛出新的InvalidOperationException(String.Format(CultureInfo.InvariantCulture, “实体不包含名为{0}”(memberName))的属性; } AssociateMetadataType(实体); var value=property.GetValue(实体,空); var context=新的ValidationContext(实体,null,null); context.MemberName=MemberName; ValidateProperty(值、上下文); } // http://buildstarted.com/2010/09/16/metadatatypeattribute-with-dataannotations-and-unit-testing/ //MetadataTypeAttribute定义的数据批注不会自动包括在内。必须注入这些定义。 私有静态void AssociateMetadataType(对象实体) { var entityType=entity.GetType(); foreach(entityType.GetCustomAttributes(typeof(MetadataTypeAttribute),true.Cast()中的var属性) { TypeDescriptor.AddProviderTransparent( 新的关联MetadataTypeDescriptionProvider(entityType,attribute.MetadataClassType),entityType); } } },c#,wpf,entity-framework,data-annotations,C#,Wpf,Entity Framework,Data Annotations,此验证器的最大缺点是: 它表现得像蜗牛。如果您在单个实体上执行它并不重要,但如果您想要处理数百、数千或更多实体,它将非常重要 它不支持开箱即用的复杂类型——您必须创建特殊属性,并在元数据中使用它来验证复杂类型 这是我最后一次使用DataAnnotation进行任何业务验证。它们主要用于只有少数实体的UI验证 用于验证复杂类型/嵌套对象的属性: /// <summary> /// Attribute for validation of nested complex type. //
- 它表现得像蜗牛。如果您在单个实体上执行它并不重要,但如果您想要处理数百、数千或更多实体,它将非常重要
- 它不支持开箱即用的复杂类型——您必须创建特殊属性,并在元数据中使用它来验证复杂类型
- 这是我最后一次使用DataAnnotation进行任何业务验证。它们主要用于只有少数实体的UI验证
/// <summary>
/// Attribute for validation of nested complex type.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class ValidateComplexTypeAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
return DataValidator.IsValid(value);
}
}
//
///用于验证嵌套复杂类型的属性。
///
[AttributeUsage(AttributeTargets.Property,AllowMultiple=false,Inherited=true)]
公共密封类ValidateComplexTypeAttribute:ValidationAttribute
{
公共覆盖布尔值有效(对象值)
{
返回DataValidator.IsValid(值);
}
}
我也遇到了同样的问题,并在
关键是调用静态函数TypeDescriptor.AddProvider()
您是否试图将
DataAnnotations
直接放在EntityFramework
生成的类上?我使用模型层来分离DB模型和应用程序模型。然后我让我的AppBaseEntity
继承IDataErrorInfo
,并在我的实体上实现DataValidations
。然后我有一个工厂将EDMX实体转换为外观。@Philippe Lavoie是的,这就是我想做的。这就是我在所有示例中看到的情况。对于一个小型应用程序来说,这似乎没那么糟糕,但如果您必须利用实体中的更多功能(如INotifyPropertyChanged
,用于特殊绑定的自定义属性等),您会发现,拥有自己的一组实体比使用自动生成的EDMX one更容易修改(它经常会影响架构更新)。顺便说一句:您可能应该使用Environment.NewLine
而不是“\r\n”sayin”。哦,谢谢。我不知道它有一个常数。谢谢,我想出了一个非常简单的解决方法,效果非常好。现在我的问题是,当抛出ValidationException
时,如何更改错误。例如,属性为整数,但用户输入字符串。我怎样才能改变这个错误。有什么想法吗?当然,把它贴在下面。可能没那么有创意,但它很简单,似乎很管用。使我不必创建和管理所有这些镜像实体。谢谢你的帮助。嘿,谢谢你的帖子。这绝对有帮助。这是我第一次使用DataAnnotation,实际上我今天才“发现”它们。我可以控制速度,我不仅要在编辑实体时一次验证一个属性,所以我应该没事。再次感谢。这是我发现的一次验证一个属性的最佳答案。但是,在web服务器的上下文中运行此操作后,服务器会在mscorlib中崩溃,并出现堆栈溢出。我已删除此验证,不再崩溃。。。在查看了TypeDescriptor.AddProviderTransparent
下的代码之后,我认为多次调用它并不重要。但是,在使用此代码时,我确实遇到了一个异常。我已将“AddProviderTransparent”调用添加到定义元数据的静态构造函数中。这可以缓解堆栈溢出的问题。
public static class EntityHelper
{
public static string ValidateProperty(object obj, string propertyName)
{
PropertyInfo property = obj.GetType().GetProperty(propertyName);
object value = property.GetValue(obj, null);
List<string> errors = (from v in property.GetCustomAttributes(true).OfType<ValidationAttribute>() where !v.IsValid(value) select v.ErrorMessage).ToList();
// I was trying to locate the source of the error
// when I print out the number of CustomAttributes on the property it only shows
// two, both of which were defined by the EF Model generator, and not the ones
// I defined in the EmployeeMetaData class
// (obj as Employee).Username = String.Join(", ", property.GetCustomAttributes(true));
return (errors.Count > 0) ? String.Join("\r\n", errors) : null;
}
}
using System.ComponentModel.DataAnnotations;
Validator.TryValidateProperty(propertyValue,
new ValidationContext(this, null, null)
{ MemberName = propertyName },
validationResults);
public static class EntityHelper
{
public static string ValidateProperty(object obj, string propertyName)
{
// get the MetadataType attribute on the object class
Type metadatatype = obj.GetType().GetCustomAttributes(true).OfType<MetadataTypeAttribute>().First().MetadataClassType;
// get the corresponding property on the MetaDataType class
PropertyInfo property = metadatatype.GetProperty(propertyName);
// get the value of the property on the object
object value = obj.GetType().GetProperty(propertyName).GetValue(obj, null);
// run the value through the ValidationAttributes on the corresponding property
List<string> errors = (from v in property.GetCustomAttributes(true).OfType<ValidationAttribute>() where !v.IsValid(value) select v.ErrorMessage).ToList();
// return all the errors, or return null if there are none
return (errors.Count > 0) ? String.Join("\r\n", errors) : null;
}
}
[MetadataType(typeof(Employee.MetaData))]
public partial class Employee:IDataErrorInfo
{
private sealed class MetaData
{
[Required(AllowEmptyStrings = false, ErrorMessage = "A name must be defined for the employee.")]
[StringLength(50, MinimumLength = 3, ErrorMessage = "The name must be between 3-50 characters long.")]
public object Name { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "A username must be defined for the employee.")]
[StringLength(20, MinimumLength = 3, ErrorMessage = "The username must be between 3-20 characters long.")]
public object Username { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "A password must be defined for the employee.")]
[StringLength(20, MinimumLength = 3, ErrorMessage = "The password must be between 3-20 characters long.")]
public object Password { get; set; }
}
public string Error { get { return String.Empty; } }
public string this[string property] { get { return EntityHelper.ValidateProperty(this, property); } }
}
// http://www.clariusconsulting.net/blogs/kzu/archive/2010/04/15/234739.aspx
/// <summary>
/// Validator provides helper methods to execute Data annotations validations
/// </summary>
public static class DataValidator
{
/// <summary>
/// Checks if whole entity is valid
/// </summary>
/// <param name="entity">Validated entity.</param>
/// <returns>Returns true if entity is valid.</returns>
public static bool IsValid(object entity)
{
AssociateMetadataType(entity);
var context = new ValidationContext(entity, null, null);
return Validator.TryValidateObject(entity, context, null, true);
}
/// <summary>
/// Validate whole entity
/// </summary>
/// <param name="entity">Validated entity.</param>
/// <exception cref="ValidationException">The entity is not valid.</exception>
public static void Validate(object entity)
{
AssociateMetadataType(entity);
var context = new ValidationContext(entity, null, null);
Validator.ValidateObject(entity, context, true);
}
/// <summary>
/// Validate single property of the entity.
/// </summary>
/// <typeparam name="TEntity">Type of entity which contains validated property.</typeparam>
/// <typeparam name="TProperty">Type of validated property.</typeparam>
/// <param name="entity">Entity which contains validated property.</param>
/// <param name="selector">Selector for property being validated.</param>
/// <exception cref="ValidationException">The value of the property is not valid.</exception>
public static void ValidateProperty<TEntity, TProperty>(TEntity entity, Expression<Func<TEntity, TProperty>> selector) where TEntity : class
{
if (selector.Body.NodeType != ExpressionType.MemberAccess)
{
throw new InvalidOperationException("Only member access selector is allowed in property validation");
}
AssociateMetadataType(entity);
TProperty value = selector.Compile().Invoke(entity);
string memberName = ((selector.Body as MemberExpression).Member as PropertyInfo).Name;
var context = new ValidationContext(entity, null, null);
context.MemberName = memberName;
Validator.ValidateProperty(value, context);
}
/// <summary>
/// Validate single property of the entity.
/// </summary>
/// <typeparam name="TEntity">Type of entity which contains validated property.</typeparam>
/// <param name="entity">Entity which contains validated property.</param>
/// <param name="memberName">Name of the property being validated.</param>
/// <exception cref="InvalidOperationException">The entity does not contain property with provided name.</exception>
/// <exception cref="ValidationException">The value of the property is not valid.</exception>
public static void ValidateProperty<TEntity>(TEntity entity, string memberName) where TEntity : class
{
Type entityType = entity.GetType();
PropertyInfo property = entityType.GetProperty(memberName);
if (property == null)
{
throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
"Entity does not contain property with the name {0}", memberName));
}
AssociateMetadataType(entity);
var value = property.GetValue(entity, null);
var context = new ValidationContext(entity, null, null);
context.MemberName = memberName;
Validator.ValidateProperty(value, context);
}
// http://buildstarted.com/2010/09/16/metadatatypeattribute-with-dataannotations-and-unit-testing/
// Data Annotations defined by MetadataTypeAttribute are not included automatically. These definitions have to be injected.
private static void AssociateMetadataType(object entity)
{
var entityType = entity.GetType();
foreach(var attribute in entityType.GetCustomAttributes(typeof(MetadataTypeAttribute), true).Cast<MetadataTypeAttribute>())
{
TypeDescriptor.AddProviderTransparent(
new AssociatedMetadataTypeTypeDescriptionProvider(entityType, attribute.MetadataClassType), entityType);
}
}
}
/// <summary>
/// Attribute for validation of nested complex type.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class ValidateComplexTypeAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
return DataValidator.IsValid(value);
}
}
using System.ComponentModel.DataAnnotations;
TypeDescriptor.AddProvider( new AssociatedMetadataTypeTypeDescriptionProvider(typeof(YourEntityClass)), typeof(YourEntityClass));