[英]Can you send 1 web service the same parameter but different names?
我们正在创建一个Web服务来接收值(字符串)。 根据字符串,我们确定它是“类型1”还是“类型2”,以及在处理方面需要做什么。 因此,我的Web服务设置为使用以下数据接收数据: http : //www.mysite.com/service.asmx?op=ProcessData?DataID=string
发送字符串的客户端希望使用2个不同的请求发送该字符串: http : //www.mysite.com/service.asmx ? op = ProcessData? DataIDType1 = string http://www.mysite.com/service.asmx?op = ProcessData?DataIDType2 =字符串
我是否可以知道他发送的是哪种类型? 我不能为此设置不同的签名吗? 因为它们都是相同的参数?
这类似于写作:
public void DoSomething(String inputA)
{
...
}
public void DoSomething(String inputB)
{
...
}
它不起作用(并且有充分的理由!)-方法签名是相同的。
想象一下电话:
MyClass.DoSomething("TEST");
会叫什么? 应该叫哪个?
您的选择(如我所见):
public void DoThingA(String input)
{
...
}
public void DoThingB(String input)
{
...
}
这将为您提供不同的方法签名,并表示您正在执行两个不同的操作s(此操作后来更干净,IMO)。
如果您坚持使用单个方法签名,则可以执行以下操作:
public void DoSomething(String input, object operationType) //where object is whatever type you see fit...
{
if(operationType == ...)
{
DoThingA(input);
}
else
{
DoThingB(input);
}
}
要么...
public void DoSomething(String input)
{
switch(input)
{
case "A":
...
break;
case "B":
...
break;
default:
...
break;
}
}
最合适的取决于您的可用选项。 但是我会创建两个方法。
一种方法应负有单一责任,因此我建议两种方法。
public void FirstMethod(string param)
{
// Do something.
}
public void SecondMethod(string param)
{
// Do something.
}
这代表了良好的设计,当客户想要添加更多功能时,后期的可维护性无疑将使您更加轻松!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.