我有MVC项目,它依赖于webservices来提供数据,这些webservices基于CMIS规范和自定义功能。我有几个用作DataContracts的类,当我添加对我打电话的服务的引用时,这些类是由Visual Studio创建的。我将该类用作模型,以确保能够将实例发送到服务并正确处理发回给我的那些实例。使用DataAnnotations验证继承的属性
我也有编辑这些类的实例的意见,我想使用DataAnnotations来验证表单(通常[Required]属性,有时显示名称更改)。
我不想把这些属性放在服务引用文件中,因为更新引用意味着我将失去这些属性(至少我不能确定引用更新后所有东西都是一样的)。
我的想法是创建子类,它只会作为工具将DataAnnotations引入到属性中,我知道我肯定会使用这些类型(肯定不会从DataContract类中消失)。我将如何完成代码的这种继承?
例子 - 我有这个类由VS在reference.cs文件中创建:
[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文件有什么变化(甚至是类本身),我会我的“编辑”和“删除”表单中始终有用户名,密码和服务器标记为[必需]。
在此先感谢
洪扎
我不愿意NAD码datacontracts因为还有很多的服务的变化,这将迫使我一个星期重写datacontracts几次。我绝对喜欢viewModel的建议,我会试一试 - 我仍然需要做一些调整,但看起来它会少得多。 – Erchi 2013-05-17 11:38:24