首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用WinForms进行实体框架UI验证

使用WinForms进行实体框架UI验证
EN

Stack Overflow用户
提问于 2013-01-22 12:05:31
回答 2查看 4K关注 0票数 19

我对使用WinForms应用程序和实体框架5设置客户端验证很感兴趣。我知道我可以实现IValidatableObject接口来执行,也有可能需要为每个实体执行自定义验证。

但是,由于我使用的是WinForms,所以我想使用ErrorProvider在用户填写表单时出现验证错误时向用户显示一个很好的通知。是否可以使用IValidatableObject接口实现此功能,或者为了让ErrorProvider正常工作,我还需要在实体上实现IDataErrorInfo接口吗?

如果你对此有任何更好的替代方案,请让我知道,我也会很乐意调查的。

EN

回答 2

Stack Overflow用户

发布于 2013-12-05 00:56:22

假设您有一个名为Car的实体,该类包含一个属性,该属性需要经过验证的

代码语言:javascript
复制
public class Car
{
  [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  public int Id { get; set; }

  // Accepted values have to be between 1 and 5.
  public int NeedToBeValidatedRange { get; set; }
}

您必须为所有实体创建一个基类,在我的示例中,我将其命名为Entity。

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;

/// This is the base class for all entities and it provide a change notfication.
public abstract class Entity : INotifyPropertyChanged
{
  // Event fired when the property is changed!
  public event PropertyChangedEventHandler PropertyChanged;


  /// Called when int property in the inherited class is changed for ther others properties like (double, long, or other entities etc,) You have to do it.
  protected void HandlePropertyChange(ref int value, int newValue, string propertyName)
  {
    if (value != newValue)
    {
      value = newValue;
      this.Validate(propertyName);
      this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
  }

  /// Validate the property 
  /// <returns>
  /// The list of validation errors
  /// </returns>
  private ICollection<ValidationResult> PropertyValidator(string propertyName)
  {
    var validationResults = new Collection<ValidationResult>();
    PropertyDescriptor property = TypeDescriptor.GetProperties(this)[propertyName];

    Validator.TryValidateProperty(
      property.GetValue(this),
      new ValidationContext(this, null, null) { MemberName = propertyName },
      validationResults);

    return validationResults;
  }

  /// Validates the given property and return all found validation errors.
  private void Validate(string propName)
  {
    var validationResults = this.PropertyValidator(propName);
    if (validationResults.Count > 0)
    {
      var validationExceptions = validationResults.Select(validationResult => new ValidationException(validationResult.ErrorMessage));
      var aggregateException = new AggregateException(validationExceptions);
      throw aggregateException;
    }
  }
}

现在你应该修改Car类,它应该是这样的:

代码语言:javascript
复制
public class Car : Entity
{
  private int id;
  private int needToBeValidatedRange;

  [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  public int Id
  {
    get
    {
      return this.id;
    }
    set
    {
      this.HandlePropertyChange(ref this.id, value, "Id");
    }
  }

  [Range(1, 5)]
  public int NeedToBeValidatedRange
  {
    get
    {
      return this.needToBeValidatedRange;
    }
    set
    {
      this.HandlePropertyChange(ref this.needToBeValidatedRange, value, "NeedToBeValidatedRange ");
    }
  }
}

在用户界面中的某处,您正在创建汽车实体:

代码语言:javascript
复制
Car car1 = new Car();
car1.NeedToBeValidatedRange = 3;  // This will work!

Car car2 = new Car();
car2.NeedToBeValidatedRange = 6;  // This will throw ValidationException

  • WPF支持非常好的ValidationException.
  • Winforms,部分支持ValidationException,但是现在您可以自由地处理这个问题。
票数 10
EN

Stack Overflow用户

发布于 2013-12-02 05:44:09

有两种选择:

  • 使用IValidateObject和IdataErrorInfo扩展poco类,并在验证方法中引发ui错误。
  • 在调用保存更改时捕获验证错误,并根据生成验证错误的实体字段直接调用ErrorProvider。

有关使用IValidateObject扩展poco类以及在调用保存更改时处理验证错误的示例,请参阅以下示例。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14451413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档