Enterprise Library 5.0,抽象基类中的验证

发布于 2024-10-21 22:44:45 字数 3199 浏览 4 评论 0原文

我的业务层使用一个名为 DomainObject 的抽象基类,它实现 IDataErrorInfo 来为 WPF 提供验证绑定。 当我调用基类中实现的“Error”属性时,没有发现错误(我的测试产生两个验证错误)。 如果我重写派生类中的属性,一切都会按预期工作,并且会发现验证错误。 我的猜测是“ValidateFromAttributes”方法中的反射有问题......?

我的示例应该返回两个错误。

这是我的代码:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Objects.DataClasses;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using ValidationResult =
    Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResult;    

public interface IDomainObject
{
    string Name { set; get; }
}

public abstract class DomainObject<T> 
    : IDataErrorInfo where T : IDomainObject
{
    protected DomainObject()
    {
    }

    protected DomainObject(EntityObject entityObject)
    {
        _entityObject = entityObject;
    }

    public string this[string columnName]
    {
        get
        {
            ValidationResults results = Validation.ValidateFromAttributes(this);

            foreach (ValidationResult result in results)
            {
                if (result.Key == columnName)
                {
                    return result.Message;
                }
            }
            return string.Empty;
        }
    }

    // *** This dosen't work and returns NO errors
    public virtual string Error
    {
        get
        {
            StringBuilder error = new StringBuilder();
            ValidationResults results = Validation.ValidateFromAttributes(this);

            foreach (ValidationResult result in results)
            {
                error.AppendLine(result.Message);
            }
            return error.ToString();
        }
    }

    private EntityObject _entityObject;
    internal EntityObject Entity
    {
        get { return _entityObject; }
        set { _entityObject = value; }
    }
}

[HasSelfValidation]
public class BoInvestment : DomainObject<BoInvestment>, 
    IDomainObject
{
    public BoInvestment(){}

    internal BoInvestment(EntityObject entityObject) : base(entityObject) {} 

    [Required]
    [StringLengthValidator(7, 
        MessageTemplate = "Name is too long")]
    public string Name { get; set; }

    [SelfValidation]
    public void Validate(ValidationResults validationResults)
    {
        if (Name != "Test")
            validationResults.AddResult(new ValidationResult(
                "Name ist falsch",this,"Name",null,null));   
    }

    // *** This works and returns two errors
    //public override string Error
    //{
    //    get
    //    {
    //        StringBuilder error = new StringBuilder();
    //        ValidationResults results = Validation.ValidateFromAttributes(this);

    //        foreach (ValidationResult result in results)
    //        {
    //            error.AppendLine(result.Message);
    //        }
    //        return error.ToString();
    //    }
    //}    
}

这是单元测试:

[TestMethod]
public void ELValidation()
{
    BoInvestment investment = new BoInvestment();
    investment.Name = "TestError";

    Console.WriteLine(investment.Error);
}

my business layer uses an abstract base class called DomainObject that implements IDataErrorInfo to offer validation-binding for WPF.
When I call the "Error" property implemented in the base class, no errors are found (my test produces two validation-errors).
If I override the property in the derived class everything woks as expected and validation errors are found.
My guess is a problem with reflection in the "ValidateFromAttributes" method...?

My sample should return two errors.

This is my Code:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data.Objects.DataClasses;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
using ValidationResult =
    Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResult;    

public interface IDomainObject
{
    string Name { set; get; }
}

public abstract class DomainObject<T> 
    : IDataErrorInfo where T : IDomainObject
{
    protected DomainObject()
    {
    }

    protected DomainObject(EntityObject entityObject)
    {
        _entityObject = entityObject;
    }

    public string this[string columnName]
    {
        get
        {
            ValidationResults results = Validation.ValidateFromAttributes(this);

            foreach (ValidationResult result in results)
            {
                if (result.Key == columnName)
                {
                    return result.Message;
                }
            }
            return string.Empty;
        }
    }

    // *** This dosen't work and returns NO errors
    public virtual string Error
    {
        get
        {
            StringBuilder error = new StringBuilder();
            ValidationResults results = Validation.ValidateFromAttributes(this);

            foreach (ValidationResult result in results)
            {
                error.AppendLine(result.Message);
            }
            return error.ToString();
        }
    }

    private EntityObject _entityObject;
    internal EntityObject Entity
    {
        get { return _entityObject; }
        set { _entityObject = value; }
    }
}

[HasSelfValidation]
public class BoInvestment : DomainObject<BoInvestment>, 
    IDomainObject
{
    public BoInvestment(){}

    internal BoInvestment(EntityObject entityObject) : base(entityObject) {} 

    [Required]
    [StringLengthValidator(7, 
        MessageTemplate = "Name is too long")]
    public string Name { get; set; }

    [SelfValidation]
    public void Validate(ValidationResults validationResults)
    {
        if (Name != "Test")
            validationResults.AddResult(new ValidationResult(
                "Name ist falsch",this,"Name",null,null));   
    }

    // *** This works and returns two errors
    //public override string Error
    //{
    //    get
    //    {
    //        StringBuilder error = new StringBuilder();
    //        ValidationResults results = Validation.ValidateFromAttributes(this);

    //        foreach (ValidationResult result in results)
    //        {
    //            error.AppendLine(result.Message);
    //        }
    //        return error.ToString();
    //    }
    //}    
}

This is the Unit Test:

[TestMethod]
public void ELValidation()
{
    BoInvestment investment = new BoInvestment();
    investment.Name = "TestError";

    Console.WriteLine(investment.Error);
}

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

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

发布评论

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

评论(1

零度° 2024-10-28 22:44:45

尝试改用 ValidationFactory。例如:

string IDomainObject.Error
{
    get
    {
        var v = ValidationFactory.CreateValidator(this.GetType());
        var results = v.Validate(this);
        return string.Join(" ",
            results.Select(r => r.Message).ToArray());
    }
}

取自此博客

Try using the ValidationFactory instead. For instance:

string IDomainObject.Error
{
    get
    {
        var v = ValidationFactory.CreateValidator(this.GetType());
        var results = v.Validate(this);
        return string.Join(" ",
            results.Select(r => r.Message).ToArray());
    }
}

Taken from this blog.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文