[英]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.