잊지 않겠습니다.

'interfaces'에 해당되는 글 1건

  1. 2010.01.04 System.ComponentModel.IDataErrorInfo
.NET MVC 및 모든 Component Model에서 Entry의 에러를 표시하는데 사용되는 Interface.
인터페이스는 각 Property에 대한 값을 검사할 수 있도록 this[columnName]에 대한 정의와 객체의 에러 상태를 알아볼 수 있는 Error 라는 string property를 지원한다.

여기에서 각 에러값은 null이 반환될 때, 에러가 없다고 가정이 되며, String.Empty가 반환되는 경우에도 에러라고 인식하게 된다.(ASP.NET MVC)

사용예는 다음과 같다.

[Table(Name="Product")]
public class Product : IDataErrorInfo 
{
    [Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
    public int ProductId { get; set; }
    [Column]
    public string Name { get; set; }
    [Column]
    public string Description { get; set; }
    [Column]
    public decimal Price { get; set; }
    [Column]
    public string Category { get; set; }

    #region IDataErrorInfo Members

    public string Error
    {
        get { return null; }
    }

    public string this[string columnName]
    {
        get
        {
            if((columnName == "Name") && string.IsNullOrEmpty(Name))
                return "Please enter a product name";
            if((columnName == "Description") && string.IsNullOrEmpty(Description))
                return "Please enter a description";
            if((columnName == "Price") && (Price <= 0))
                return "Price must not be negative or zero";
            if((columnName == "Category") && string.IsNullOrEmpty(Category))
                return "Please specify a category";
            return null;
        }
    }

    #endregion
}
Posted by Y2K
,