使用的WinForms实体框架UI验证 [英] Entity Framework UI Validation using WinForms

查看:307
本文介绍了使用的WinForms实体框架UI验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感兴趣的是建立使用WinForms应用程序和Entity Framework 5。据我所知,还有的IValidatableObject接口,我可以实现执行,而且我可能需要为每个实体的自定义验证客户端验证。

I'm interested in setting up client side validation using a WinForms application and Entity Framework 5. I understand that there's the IValidatableObject interface that I can implement to perform and custom validation that I may need for each entity.

不过,由于我使用的WinForms,我想用ErrorProvider一个不错的通知,向用户时,有一个验证错误,因为他们填写一份形成。这是功能能够使用IValidatableObject接口可以实现或者我需要实现我的实体IDataErrorInfo的接口,以及为了有ErrorProvider正常工作?

However, since I'm using WinForms I'd like to use the ErrorProvider to present the user with a nice notification when there is a validation error as they fill out a form. Is this functionality able to be achieved using the IValidatableObject interface or would I need to implement the IDataErrorInfo interface on my entities as well in order to have the ErrorProvider work properly?

如果你有这个一个更好的选择,任何其他建议,请让我知道,我会很乐意考虑这一点。

If you have any other suggestions on a better alternative to this please let me know and I'll gladly look into that as well.

推荐答案

比方说你有一个实体名为这个类包含了必要时的验证

Lets say you have an Entity called Car and this class contains an property which need be validated.

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; }
}

您必须创建在我的例子中所有的entites一个基类我。将所谓的实体

You have to create a base class for all your entites in my example I will called Entity.

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;
    }
  }
}

现在你将modfiy的车一流的,它应该是这样的:

Now you shall modfiy the Car class and it should be like that:

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 ");
    }
  }
}



某处在用户界面会正在创建汽车实体:

Somewhere in the user interface you are creating the car entities:

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但现在你可以自由如何处理这个问题。

  • 这篇关于使用的WinForms实体框架UI验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆