gpt4 book ai didi

.net - ASP.NET MVC 和 ADO.NET Entity Framework 中的最佳实践实体验证

转载 作者:行者123 更新时间:2023-12-01 12:01:18 25 4
gpt4 key购买 nike

我在项目中使用 ASP.NET MVC 和 ADO.NET Entity Framework 。我想通过部分类向我的实体添加验证逻辑。它的工作原理类似于 NerdDinner.com ASP.NET MVC Application 中所示这是使用 LINQ2SQL。主要区别在于,我必须使用“OnPropertyChanging”事件而不是像 LINQ2SQL 中那样的“OnValidating”。

这样做会遇到一些问题:- “OnPropertyChanging”事件不是调用验证逻辑的最佳点,因为它总是触发,即使在创建调用默认构造函数时也是如此。这确实会导致严重的问题(不仅仅是性能问题)。- 与 MVC 框架一起使用“EntityState.Detached”(我找不到任何其他方法)来确定实体是否需要验证时存在问题。因为实体在 View 中显示期间会丢失其实体状态(因为在 POST 事件中会创建一个新的实体对象而不是返回原始对象)。

我的问题是:是否有更好的方法向 ADO.NET 实体添加验证?我找不到任何使用向 ADO.NET 实体添加验证的实用方法的教程。

最佳答案

就我个人而言,我不会对对象本身进行验证。我用 xVal处理我的实体验证的库。

xVal 鼓励您使用描述各种验证规则的属性来注释您的实体类(或者,实际上,元数据伴随类)。这些验证属性是 System.ComponentModel.DataAnnotations 中 .NET 附带的默认属性。

然后您可以在业务层中手动对您的对象运行验证。这是通过使用运行 System.ComponentModel.DataAnnotations 验证逻辑的方法来完成的。我写了一个看起来像这样的:

/// <summary>
/// Gets the validation errors for the passed in object by using reflection to retrieve the
/// <see cref="ValidationAttribute"/>s placed on its properties or on the properties of the object's
/// metadata class (as specified by a <see cref="MetadataTypeAttribute"/> placed on the object's class)
/// </summary>
/// <param name="instance">The object to validate</param>
/// <returns>Any validation errors</returns>
/// <remarks>
/// Borrowed (and cleaned up) from
/// http://goneale.com/2009/03/04/using-metadatatype-attribute-with-aspnet-mvc-xval-validation-framework/
/// </remarks>
public static IEnumerable<ErrorInfo> Validate(object instance)
{
//Try to get the MetadataType attribute from the object
MetadataTypeAttribute metadataAttrib = instance.GetType().GetCustomAttributes(typeof(MetadataTypeAttribute), true).OfType<MetadataTypeAttribute>().FirstOrDefault();

//If the MetadataType attribute existed, get the metadata class
//else just use the class of the object
Type buddyClassOrModelClass = metadataAttrib != null ? metadataAttrib.MetadataClassType : instance.GetType();

IEnumerable<PropertyDescriptor> buddyClassProperties = TypeDescriptor.GetProperties(buddyClassOrModelClass).Cast<PropertyDescriptor>();
IEnumerable<PropertyDescriptor> modelClassProperties = TypeDescriptor.GetProperties(instance.GetType()).Cast<PropertyDescriptor>();

//This query matches each property on the model class against the buddy class
//gets a list of all invalid validation attributes and returns a list of
//validation errors
return from buddyProp in buddyClassProperties
join modelProp in modelClassProperties on buddyProp.Name equals modelProp.Name
from attribute in buddyProp.Attributes.OfType<ValidationAttribute>()
where !attribute.IsValid(modelProp.GetValue(instance))
select new ErrorInfo(buddyProp.Name, attribute.FormatErrorMessage(String.Empty), instance);
}

xVal 提供了一个简洁的异常类型,您可以抛出它来封装验证错误,并允许您轻松地将它们添加到 Controller 中的 ModelState。

xVal 还将通过提供 HtmlHelper 方法利用 jQuery.Validate 自动生成客户端 JavaScript 表单验证代码。

查看 http://blog.codeville.net/2009/01/10/xval-a-validation-framework-for-aspnet-mvc/有关其工作原理的演练。我发现它是一种非常好的验证方式,而不是一件繁琐的事情。它非常适合 ASP.NET MVC 的做事方式。

关于.net - ASP.NET MVC 和 ADO.NET Entity Framework 中的最佳实践实体验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1142083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com