如何在实体外部进行数据注释

时间:2012-07-19 08:07:21

标签: wpf entity-framework wpf-controls entity data-annotations

我在我的项目中使用Wpf和EntityFrameWork。我想使用DataAnnotations,因为它是验证数据的更好方法。我设法在我的实体文件中创建它。例如,我的一个实体包括:

public partial class Employee : INotifyPropertyChanged, IDataErrorInfo
{
    public Employee()
    {
        this.Address = new HashSet<Address>();
        this.Certificate = new HashSet<Certificate>();
        this.ContactInformation = new HashSet<ContactInformation>();
        this.Course = new HashSet<Course>();
        this.Education = new HashSet<Education>();
        this.EmployeeTeam = new HashSet<EmployeeTeam>();
        this.Evaluation = new HashSet<Evaluation>();
        this.ExamInformation = new HashSet<ExamInformation>();
        this.JobTitle = new HashSet<JobTitle>();
        this.MilitaryService = new HashSet<MilitaryService>();
        this.Relationship = new HashSet<Relationship>();
        this.WorkExperience = new HashSet<WorkExperience>();
    }

    public int Id { get; set; }


    private string _CitizenId;

    [Required(ErrorMessage = "CitizenId is required!")]
    [RegularExpression(@"^\d+$", ErrorMessage = "Only numbers allowed")]
    public string CitizenId
    {
        get
        {
            return _CitizenId;
        }

        set
        {
            _CitizenId = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("CitizenId"));
            }
        }
    }


    private int _RegNumber;
    [Required (ErrorMessage = "Registration Number is required!")]
    [RegularExpression(@"^\d+$", ErrorMessage = "Only numbers allowed")]

    public int RegNumber
    {
        get
        {
            return _RegNumber;
        }

        set
        {
            _RegNumber = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("RegNumber"));
            }
        }
    }
    public Nullable<short> ResourceTypeId { get; set; }
    public Nullable<short> EmployeeRoleId { get; set; }


    private string _FirstName;

    [Required(ErrorMessage="Name is required!")]
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage="Numeric expressions are not allowed!")]



    public string FirstName
    {
        get
        {
            return _FirstName;
        }

        set
        {
            _FirstName = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
            }
        }
    }

    private string _MiddleName;
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numeric expressions are not allowed!")]

    public string MiddleName
    {
        get
        {
            return _MiddleName;
        }

        set
        {
            _MiddleName = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("MiddleName"));
            }
        }
    }


    private string _Surname;
    [Required(ErrorMessage= "Surname is required!")]
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numeric expressions are not allowed!")]



    public string Surname {

        get
        {
            return _Surname;
        }

        set
        {
            _Surname = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Surname"));
            }
        }
    }


    private int _CustomerNo;
    [Required(ErrorMessage = "Customer Number is required!")]
    [RegularExpression(@"^\d+$", ErrorMessage = "Only numbers allowed")]
    public int CustomerNo
    {
        get
        {
            return _CustomerNo;
        }

        set
        {
            _CustomerNo = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("CustomerNo"));
            }
        }
    }


    public System.DateTime BirthDate { get; set; }



    private string _BirthPlace;
    [Required(ErrorMessage="Birh Place is required!")]
    [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numeric expressions are not allowed!")]

    public string BirthPlace
    {
        get
        {
            return _BirthPlace;
        }

        set
        {
            _BirthPlace = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("BirthPlace"));
            }
        }
    }
    public string Gender { get; set; }
    public string MaritalStatus { get; set; }
    public System.DateTime WorkBeginDate { get; set; }

    //indicates that whether the start  date is bigger than the end date which is a validation problem
  //  [CustomValidationsForDate("WorkBeginDate", ErrorMessage="Starting date cannot be bigger than the ending date")]
    public Nullable<System.DateTime> WorkEndDate { get; set; }

    private string _LogonName;
    [Required(ErrorMessage = "Logon Name is required!")]

    public string LogonName
    {
        get
        {
            return _LogonName;
        }

        set
        {
            _LogonName = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("LogonName"));
            }
        }
    }

    private int _DistanceToWork;
    [RegularExpression(@"^\d+$", ErrorMessage = "Only numbers allowed")]
    [Required(ErrorMessage="This field is required!")]
    public int DistanceToWork
    {
        get
        {
            return _DistanceToWork;
        }

        set
        {
            _DistanceToWork = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("DistanceToWork"));
            }
        }
    }
    public Nullable<bool> HasInternet { get; set; }
    public Nullable<bool> HasRemoteConnection { get; set; }
    public int MilitaryStatus { get; set; }
    public Nullable<int> DrivingLicense { get; set; }
    public bool IsSmoker { get; set; }


    public bool IsActive
    { get; set; }
    public Nullable<System.DateTime> CreateDate { get; set; }
    public string LeavingReason { get; set; }
    public Nullable<System.DateTime> StartDate { get; set; }




    public Nullable<System.DateTime> EndDate { get; set; }

    public virtual ICollection<Address> Address { get; set; }
    public virtual ICollection<Certificate> Certificate { get; set; }
    public virtual ICollection<ContactInformation> ContactInformation { get; set; }
    public virtual ICollection<Course> Course { get; set; }
    public virtual ICollection<Education> Education { get; set; }
    public virtual ICollection<EmployeeTeam> EmployeeTeam { get; set; }
    public virtual ICollection<Evaluation> Evaluation { get; set; }
    public virtual ICollection<ExamInformation> ExamInformation { get; set; }
    public virtual ICollection<JobTitle> JobTitle { get; set; }
    public virtual ICollection<MilitaryService> MilitaryService { get; set; }
    public virtual ICollection<Relationship> Relationship { get; set; }
    public virtual ICollection<WorkExperience> WorkExperience { get; set; }


    //Following methos have been used in order to apply DataAnnotations


    public event PropertyChangedEventHandler PropertyChanged;

    public string Error { get { return String.Empty; } }

    public string this[string property]
    {
        get { return ValidateProperty(this, property); }
    }

    public static string ValidateProperty(object instance, string propertyName)
    {
        PropertyInfo property = instance.GetType().GetProperty(propertyName);
        object value = property.GetValue(instance, null);
        List<string> errors = (from v in property.GetCustomAttributes(true).OfType<ValidationAttribute>()
                               where !v.IsValid(value)
                               select v.ErrorMessage).ToList();
        return (errors.Count > 0) ? String.Join("\r\n", errors) : null;
    }


}
}

就像我说我在实体中应用数据注释但我想要的是在外面做,因为无论何时在我的数据库中发生更改,所有实体都将被刷新,因此我在这里所拥有的不是动态结构。 你能帮忙吗?

提前致谢。 最好的问候。

埃格

1 个答案:

答案 0 :(得分:0)

您应该创建一个模型(视图模型)类,并从您的实体(域模型)映射到视图模型。 AutoMapper可以帮助加快速度。然后,将注释放在视图模型上,并在视图中使用这些注释。

这意味着如果您的实体模型发生更改,您只需更新映射即可。这也意味着您可以为同一个实体创建多个不同的模型,因此对于使用它的每个视图,您的验证不必相同。