0

I have MVC project that relies on webservices to provide data and those webservices are based on CMIS specification with custom functionality. I have several classes used as DataContracts, which were created by Visual Studio when I added references to services I am calling. I am using that class as a model to ensure I am able to send instances to the service and process correctly those sent back to me.

I also have views to edit instances of those classes and I would like to use DataAnnotations to validate the forms (usually [Required] atribute and sometimes display name change).

I do not want to put those atributes in service reference files because updating the reference would mean I will loose those atributes (at least I could not be sure everything is still the same after reference update).

My thought was to create child class that would only serve as tool to introduce DataAnnotations to atributes I know for sure I will be using (those that will not dissapear from DataContract class for sure). How would I accomplish such inheritance with code?

Example - I have this class created by VS in reference.cs file:

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

I want to make sure that no matter what changes in reference.cs file (even that class itself), I will always have Username, Password and Server marked as [Required] in my "Edit" and "Delete" forms.

Thanks in advance

Honza

4

1 回答 1

0

我会远离继承自动生成的类。它不会解决您的属性问题 - 您必须覆盖每个属性,以便您可以向其添加属性。

一种解决方案是使用手动编码的数据合约而不是自动生成的引用。您将完全控制它们何时更改,并且可以将所需的属性放入其中。

另一种解决方案是将合同包装在您的视图模型中。像这样:

 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
  }
于 2013-05-14T15:24:37.787 回答