参考我之前的问题,故事开始于我有一堆 svcutil 生成的类。它们是从外部 WSDL 生成的。干得好:
第一个请求类:
public partial class getcarsRequest
{
[System.ServiceModel.MessageHeaderAttribute(Namespace = "http://svc.datadomains.com/revision123_2/")]
public CarsServiceApp.RequestHeader Header;
[System.ServiceModel.MessageBodyMemberAttribute(Name = "getcarsRequest", Namespace = "carinfo", Order = 0)]
public CarsServiceApp.getcars MessageWrap;
public getcarsRequest()
{
}
public getcarsRequest(CarsServiceApp.RequestHeader Header, CarsServiceApp.getcars getcarsRequest1)
{
this.Header = Header;
this.MessageWrap = getcarsRequest1;
}
}
public partial class getcars
{
private MessageType messageField;
private MessageDataGetcarsRequest messageDataField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 0)]
public MessageType Message
{
get
{
return this.messageField;
}
set
{
this.messageField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 1)]
public MessageDataGetcarsRequest MessageData
{
get
{
return this.messageDataField;
}
set
{
this.messageDataField = value;
}
}
}
public partial class MessageDataGetcarsRequest
{
private AppDataGetcarsRequest appDataField;
private AppDocumentType appDocumentField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 0)]
public AppDataGetcarsRequest AppData
{
get
{
return this.appDataField;
}
set
{
this.appDataField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 1)]
public AppDocumentType AppDocument
{
get
{
return this.appDocumentField;
}
set
{
this.appDocumentField = value;
}
}
}
public partial class AppDataGetcarsRequest
{
private string addressField;
private int codeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 0)]
public address address
{
get
{
return this.addressField;
}
set
{
this.addressField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 1)]
public int code
{
get
{
return this.codeField;
}
set
{
this.codeField = value;
}
}
}
第二:
public partial class getdriversRequest
{
[System.ServiceModel.MessageHeaderAttribute(Namespace = "http://svc.datadomains.com/revision123_2/")]
public carsServiceApp.RequestHeader Header;
[System.ServiceModel.MessageBodyMemberAttribute(Name = "getdriversRequest", Namespace = "driverinfo", Order = 0)]
public carsServiceApp.getdrivers MessageWrap;
public getdriversRequest()
{
}
public getdriversRequest(carsServiceApp.RequestHeader Header, carsServiceApp.getdrivers getdriversRequest1)
{
this.Header = Header;
this.MessageWrap = getdriversRequest1;
}
}
public partial class getdrivers
{
private MessageType messageField;
private MessageDataGetdriversRequest messageDataField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 0)]
public MessageType Message
{
get
{
return this.messageField;
}
set
{
this.messageField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 1)]
public MessageDataGetdriversRequest MessageData
{
get
{
return this.messageDataField;
}
set
{
this.messageDataField = value;
}
}
}
public partial class MessageDataGetdriversRequest
{
private AppDataGetdriversRequest appDataField;
private AppDocumentType appDocumentField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 0)]
public AppDataGetdriversRequest AppData
{
get
{
return this.appDataField;
}
set
{
this.appDataField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 1)]
public AppDocumentType AppDocument
{
get
{
return this.appDocumentField;
}
set
{
this.appDocumentField = value;
}
}
}
public partial class AppDataGetdriversRequest
{
private string nameField;
private int customerCodeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 0)]
public name name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 1)]
public int customerCode
{
get
{
return this.customerCodeField;
}
set
{
this.customerCodeField = value;
}
}
}
这只是 svcutil 生成的两个实体。还有其他实体与这两个类似,仅在大多数基础AppData
属性上有所不同。我编写了一个强大的 shell 脚本来准备原始生成的文件重命名某些字段,但这还不足以完成所有工作。
我怎样才能组成班级团结?看来我应该使用参数化接口......我需要统一的类结构来设计常见的有用功能,例如检查请求是否正确或从头开始创建请求。
提前谢谢各位!我的大脑都为那些东西沸腾了。
问题编辑#1
好的,伙计们,这是我想要的。假设我们要检查任何服务方法的正确性请求。如果某个请求的AppData
属性不为空,我们应该认为该请求是正确的。实际上,我们最好有一些通用的类方法来进行这种检查。AppData
但是,如果任何请求类具有不同的属性类型,我们如何创建该方法呢?
让我们看一下两个生成的类,并为每个 AppData 属性绘制一些假想的路径。
对于第一类,我们有 getcarsRequest(在括号中,我们有适当的类类型):
请求(getcarsRequest)-> MessageWrap(getcars)-> MessageData(MessageDataGetcarsRequest)-> AppData(AppDataGetcarsRequest)
对于第二个我们有下一条路径:
请求(getdriversRequest)-> MessageWrap(getdrivers)-> MessageData(MessageDataGetdriversRequest)-> AppData(AppDataGetdriversRequest)
那么我们如何重新设计并将它们简化为一些通用接口呢?如果我们为这两个类有一个合适的通用接口,我们可以编写一些CheckRequest(IRequest<T> request)
.
我希望我在这里得到一些澄清。任何建议/句子将不胜感激。如果您对我有任何疑问,请随时将它们带给我。