0

プロジェクトで Wpf と EntityFrameWork を使用しています。データを検証するためのより良い方法であるため、DataAnnotations を使用したいと思いました。私は自分のエンティティファイルでそれを作ることができました。たとえば、私のエンティティの 1 つには次のものが含まれます。

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


}
}

私が言ったように、エンティティ内にデータ注釈を適用していますが、データベースで変更が発生するたびにすべてのエンティティが更新されるため、外部でそれを実行したいので、ここにあるのは動的構造ではありません。それで、助けていただけますか?

前もって感謝します。よろしくお願いします。

エゲ

4

1 に答える 1

0

モデル (ビュー モデル) クラスを作成し、エンティティ (ドメイン モデル) からビュー モデルにマップする必要があります。AutoMapper は、これを高速化するのに役立ちます。次に、注釈をビュー モデルに配置し、これらをビューで使用します。

つまり、エンティティ モデルが変更された場合、マッピングを更新するだけで済みます。また、同じエンティティに対して複数の異なるモデルを作成できるため、それを使用するビューごとに検証が同じである必要はありません。

于 2012-07-19T09:42:54.590 に答える