简体   繁体   中英

Validating inherited attribute using DataAnnotations

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

I would stay away from inheriting an autogenerated class. It would not solve your problem with the attributes - you would have to override every single property so you can add attributes to it.

One solution is to use hand-coded datacontracts instead of autogenerated references. You will have full control over when they change, and you can put the attributes you need in them.

Another solution is wrapping the contract in your view model. Like this:

 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
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM