gpt4 book ai didi

c# - 使用 Microsoft EnterpriseLibrary 验证时堆栈溢出

转载 作者:太空宇宙 更新时间:2023-11-03 22:27:51 24 4
gpt4 key购买 nike

我有两个类:

[HasSelfValidation]
class Country : DomainObject
{
[NotNullValidator]
[StringLengthValidator(2, 10)]
public string Name { get; set; }

[ObjectCollectionValidator(typeof(Region))]
public List<Region> Regions { get; set; }
}

[HasSelfValidation]
class Region : DomainObject
{
[NotNullValidator]
[ObjectValidator]
public Country Country { get; set; }

[NotNullValidator]
[StringLengthValidator(2, 20)]
public string Name { get; set; }
}

DomainObject 有方法的地方:

public virtual ValidationResults Validate()
{
Validator validator =
ValidationFactory.CreateValidator(this.GetType());
ValidationResults results = new ValidationResults();
validator.Validate(this, results);
return results;
}

我使用的是 Microsoft Enterprise Library 4.1 - October 2008/.NET 3.5 SP1/Vista。

如果我为新创建的 Country 对象调用 Validate 并将 null 作为区域列表,我会收到 StackOverflow 异常。如果我删除 Country.Regions 属性的 [ObjectCollectionValidator(typeof(Region))],一切正常。我想链接 Country - Region - Country 是失败的原因。但是,我不想删除 Regions 集合的验证;从区域中删除 [ObjectValidator] 对我来说都不是一个选项。我可以做些什么来维护所有这些验证属性,没有 StackOverflow 异常吗?

谢谢,

卢锡安

最佳答案

前几天我遇到了同样的问题,并且能够通过实现一个自定义验证器类来解决这个问题,该验证器类的功能与 ObjectValidator 非常相似,只是它延迟了对正在评估的属性的评估,直到实际的 DoValidate 方法,如果该属性为 null,则不会继续构建验证器。

using System;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Validation.Properties;

namespace Microsoft.Practices.EnterpriseLibrary.Validation.Validators
{
/// <summary>
/// Performs validation on objects by applying the validation rules specified for a supplied type at RUNTIME.
/// This validator can be used to get past StackOverflowExceptions that can be thrown as a result of the design
/// of the ObjectValidator attribute
/// </summary>
/// <seealso cref="ValidationFactory"/>
public class RuntimeObjectValidator : Validator
{
private Type targetType;
private string targetRuleset;

/// <summary>
/// <para>Initializes a new instance of the <see cref="RuntimeObjectValidator"/> for a target type.</para>
/// </summary>
/// <param name="targetType">The target type</param>
/// <remarks>
/// The default ruleset for <paramref name="targetType"/> will be used.
/// </remarks>
/// <exception cref="ArgumentNullException">when <paramref name="targetType"/> is <see langword="null"/>.</exception>
public RuntimeObjectValidator(Type targetType)
: this(targetType, string.Empty)
{ }

/// <summary>
/// <para>Initializes a new instance of the <see cref="RuntimeObjectValidator"/> for a target type
/// using the supplied ruleset.</para>
/// </summary>
/// <param name="targetType">The target type</param>
/// <param name="targetRuleset">The ruleset to use.</param>
/// <exception cref="ArgumentNullException">when <paramref name="targetType"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentNullException">when <paramref name="targetRuleset"/> is <see langword="null"/>.</exception>
public RuntimeObjectValidator(Type targetType, string targetRuleset)
: base(null, null)
{
if (targetType == null)
{
throw new ArgumentNullException("targetType");
}
if (targetRuleset == null)
{
throw new ArgumentNullException("targetRuleset");
}

this.targetType = targetType;
this.targetRuleset = targetRuleset;
}

/// <summary>
/// Validates by applying the validation rules for the target type specified for the receiver.
/// </summary>
/// <param name="objectToValidate">The object to validate.</param>
/// <param name="currentTarget">The object on the behalf of which the validation is performed.</param>
/// <param name="key">The key that identifies the source of <paramref name="objectToValidate"/>.</param>
/// <param name="validationResults">The validation results to which the outcome of the validation should be stored.</param>
/// <remarks>
/// If <paramref name="objectToValidate"/> is <see langword="null"/> validation is ignored.
/// <para/>
/// A referece to an instance of a type not compatible with the configured target type
/// causes a validation failure.
/// </remarks>
protected internal override void DoValidate(object objectToValidate,
object currentTarget,
string key,
ValidationResults validationResults)
{
if (objectToValidate != null)
{
if (this.targetType.IsAssignableFrom(objectToValidate.GetType()))
{
validationResults.AddAllResults(
ValidationFactory.CreateValidator(objectToValidate.GetType()).Validate(objectToValidate));
}
else
{
// unlikely
this.LogValidationResult(validationResults, Resources.ObjectValidatorInvalidTargetType, currentTarget, key);
}
}
}

/// <summary>
/// Gets the message template to use when logging results no message is supplied.
/// </summary>
protected override string DefaultMessageTemplate
{
get { return null; }
}

#region test only properties

internal Type TargetType
{
get { return this.targetType; }
}

internal string TargetRuleset
{
get { return this.targetRuleset; }
}

#endregion
}
}

当然,您还需要创建一个 RuntimeObjectValidatorAttribute 类,这样您就可以执行以下操作:

public class AClassThatReferencesItself
{
private AClassThatReferencesItself _other;

private string myString;

[NotNullValidator]
public string MyString
{
get { return myString; }
set { myString = value; }
}


[RuntimeObjectValidator]
[NotNullValidator]
public AClassThatReferencesItself Other
{
get { return _other; }
set { _other = value; }
}

}

关于c# - 使用 Microsoft EnterpriseLibrary 验证时堆栈溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/578279/

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