簡體   English   中英

使用DataAnnotations驗證繼承的屬性

[英]Validating inherited attribute using DataAnnotations

我有一個MVC項目,該項目依賴於Web服務來提供數據,並且那些Web服務基於具有自定義功能的CMIS規范。 我有幾個用作DataContracts的類,它們是在我添加對要調用的服務的引用時由Visual Studio創建的。 我使用該類作為模型,以確保能夠將實例發送到服務並正確處理發送回給我的實例。

我也有視圖來編輯這些類的實例,我想使用DataAnnotations來驗證表單(通常是[Required]屬性,有時還會顯示名稱更改)。

我不想將這些屬性放入服務引用文件中,因為更新引用將意味着我將失去這些屬性(至少我無法確定在引用更新后所有內容仍然相同)。

我的想法是創建子類,該子類僅用作將DataAnnotations引入我肯定會使用的屬性的工具(肯定不會從DataContract類中消失的那些子類)。 我將如何用代碼完成這種繼承?

示例-我在reference.cs文件中有VS創建的此類:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="LibraryRequest", Namespace="http://schemas.datacontract.org/2004/07/Agamemnon.Models")]
[System.SerializableAttribute()]
public partial class LibraryRequest : DocuLive.RepositoryServiceExt.Library {

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string PasswordField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string ServerField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private bool UseDefaultField;

    [System.Runtime.Serialization.OptionalFieldAttribute()]
    private string UserNameField;

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string Password {
        get {
            return this.PasswordField;
        }
        set {
            if ((object.ReferenceEquals(this.PasswordField, value) != true)) {
                this.PasswordField = value;
                this.RaisePropertyChanged("Password");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string Server {
        get {
            return this.ServerField;
        }
        set {
            if ((object.ReferenceEquals(this.ServerField, value) != true)) {
                this.ServerField = value;
                this.RaisePropertyChanged("Server");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public bool UseDefault {
        get {
            return this.UseDefaultField;
        }
        set {
            if ((this.UseDefaultField.Equals(value) != true)) {
                this.UseDefaultField = value;
                this.RaisePropertyChanged("UseDefault");
            }
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public string UserName {
        get {
            return this.UserNameField;
        }
        set {
            if ((object.ReferenceEquals(this.UserNameField, value) != true)) {
                this.UserNameField = value;
                this.RaisePropertyChanged("UserName");
            }
        }
    }
}

我想確保無論reference.cs文件中有什么更改(甚至是該類本身),在我的“編輯”和“刪除”表格中,我總是將用戶名,密碼和服務器標記為[必需]。

提前致謝

本za

我將避免繼承自動生成的類。 它不能解決您的屬性問題-您將不得不覆蓋每個屬性,以便可以向其中添加屬性。

一種解決方案是使用手動編碼的數據合同,而不是自動生成的引用。 您將完全控制它們何時更改,並且可以在其中放置所需的屬性。

另一種解決方案是將合同包裝在您的視圖模型中。 像這樣:

 public class LibraryRequestViewModel  {
     private LibraryRequest request;

     public LibraryRequestViewModel(LibraryRequest request){
          this.request = request;
     }

     [Required]
     public string Password {
         get { return this.request.Password; }
         set { this.request.Password = value; }
     }
     // do this for all fields you need
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM