【发布时间】:2023-03-27 20:05:01
【问题描述】:
我在我的项目中使用 Windows 通信服务 (WCF)。
在我的项目中,
我写的函数如下:
GetUserNameByUserId(int userId);
GetProductInformationByProductId(int productId);
但是这个命名已经一天比一天复杂了。
例如,我有 5 个参数要传递给函数,在这种情况下,函数名称将类似于:
GetStackOverFlowByStackByOverByFlowByIdByStackOverFlow(string stack, string over, string flow, int id, string stackOverFlow);
并假设我想获得 2 个参数,例如打击:
GetStackOverFlowByIdByStackOverFlow(int id, string stackOverFlow);
我想使用如下函数重载:
public void abc(int i)
{
System.Console.WriteLine("abc" + i);
}
public void abc(string i)
{
System.Console.WriteLine("abc" + i);
}
public void abc(string i,int j)
{
System.Console.WriteLine("abc" + i + j);
}
也就是说,我想写下面的函数:
GetStackOverFlow(int id);
GetStackOverFlow(int id, string name);
GetStackOverFlow(int id, string name, string StackOver);
.
.
不是吗? 有什么方法吗? 还是我做得对?
我研究并发现: Function Overloading in WCF
public interface IMyService
{
[OperationContract(Name = "GetStringWithParam")]
string GetString(DateTime date);
[OperationContract(Name = "GetStringWithoutParam")]
string GetString();
}
他说
但我不喜欢它,因为它有时会导致混乱。
还有其他方法吗? 谢谢。
【问题讨论】:
-
你为什么要写函数
GetStackOverFlow(id, name)来过滤id和名字?我假设“id”已经只返回一个对象。你还能过滤多少? -
好方法,但不要认为只有 id 有时一个 Id 可以有多行,所以我必须使用更多过滤器..