您的位置:首页 > 其它

Data Annotation

2016-06-21 07:17 211 查看

Data Annotation

什么是Data Annotation ?

如何使用 ?

自定义Validate Attribute

EF Db first中使用Data Annotation

asp.net MVC中使用Data Annotation

什么是Data Annotation ?

貌似没听过,但肯定见过

所属程序集:System.ComponentModel.DataAnnotations

DataAnnotation code:

public class Product
{

[Required]
[StringLength(10,MinimumLength =5)]
public string Name { get; set; }

[Required]
public decimal? UnitPrice { get; set; }
}


没错,就是给类的属性加上描述性的验证信息,

如何使用这些信息 为我们自己所用呢?

当然是先自己想办法了,

添加辅助类:

public class ModelValidationError
{
public string FieldName { get; set; }
public string Message { get; set; }
}
public static class DataAnnotationHelper
{
public static IEnumerable<ModelValidationError> IsValid<T>(this T o)
{
var descriptor = GetTypeDescriptor(typeof(T));

foreach (PropertyDescriptor propertyDescriptor in descriptor.GetProperties())
{
var validations = propertyDescriptor.Attributes.OfType<ValidationAttribute>();
foreach (var validationAttribute in validations)
{
var v = propertyDescriptor.GetValue(o);

if (!validationAttribute.IsValid(v))
{
yield return new ModelValidationError() { FieldName = propertyDescriptor.Name, Message = validationAttribute.FormatErrorMessage(propertyDescriptor.Name) };
}
}
}
}
private static ICustomTypeDescriptor GetTypeDescriptor(Type type)
{
return new AssociatedMetadataTypeTypeDescriptionProvider(type).GetTypeDescriptor(type);
}
}


如何使用:

class Program
{
static void Main(string[] args)
{
Product product = new Product();
foreach (var item in product.IsValid())
{
Console.WriteLine("FieldName:{0} Error Message:{1}", item.FieldName, item.Message);
}
Console.ReadKey();
}
}


自定义ValidateAttribute

.net 提供的 ValidateAttribute不够用怎么搞?自定义呗,

public class PriceAttribute : ValidationAttribute
{
public double MinPrice { get; set; }

public override bool IsValid(object value)
{
if (value == null)
{
return false;
}
var price = (double)value;

if (price < MinPrice)
{
return false;
}
return true;
}
public override string FormatErrorMessage(string name)
{
return "Min Price is "+MinPrice;
}
}


使用方法和.net 提供的一样:

public class Product
{

[Required]
[StringLength(10,MinimumLength =5)]
public string Name { get; set; }

[Required]
[Price(MinPrice =2)]
public decimal? UnitPrice { get; set; }
}


EF Db first中使用Data Annotation

实际应用中遇到的问题:

在使用EF DBfirst的时候,实体类的validate attribute,一不小心经常会被覆盖掉,如何解决

巧妙使用partial 类

public class ProductMetaData
{
[Required]
[StringLength(10, MinimumLength = 5)]
public string Name { get; set; }

[Required]
[Price(MinPrice = 2)]
public decimal? UnitPrice { get; set; }
}
[MetadataType(typeof(ProductMetaData))]
public partial class Product
{

}
public partial class Product
{
public string Name { get; set; }

public decimal? UnitPrice { get; set; }
}


asp.net mvc 中data annotation的使用:

asp.net mvc中对data annotation具有原生的支持,

默认情况下,ASP.NET MVC框架在模型绑定时执行验证逻辑。
模型邦定器一旦完成对模型属性的更新,就会利用当前的模型元数据获得模型的所有验证器。这运行时提供了一个验证其 (DataAnnotationModelValidator)来与注解一同工作。这个模型验证器会找到所有的验证特性并执行它们包含的验证逻辑。 ModelBinder捕获所有失败的验证其规则并把它们放入ModelState中。

模型绑定的主要产物就是ModelState(Controller的属性)。
这个对象不仅包含了用户所有想放入模型属性里的值,也包括与每一个属性相关联的所有错误,和模型本身的错误,如果存在错误ModelState.IsValid返回false。

ModelState("LastName").Errors[0].ErrorMessage;//查看LastName属性的错误信息
View中查看:@Html.ValadationMessageFor(m=>m.LastName)

在编辑操作的PostAction中,可以先使用ModelState.IsValid属性判断是否通过验证,再不同对待。



参考文档: http://www.asp.net/mvc/overview/older-versions-1/models-data/validation-with-the-data-annotation-validators-cs http://www.cnblogs.com/xinchuang/archive/2013/06/06/3120482.html http://www.cnblogs.com/kevin-kingdom/archive/2012/12/07/2807138.html http://www.cnblogs.com/hjf1223/archive/2010/11/07/independent-dataannotation-validation.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: