Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用WCF时,函数重载规则?_C#_Wcf_C# 4.0 - Fatal编程技术网

C# 使用WCF时,函数重载规则?

C# 使用WCF时,函数重载规则?,c#,wcf,c#-4.0,C#,Wcf,C# 4.0,我在项目中使用Windows Communication Services(WCF) 在我的项目中 我编写的函数如下所示: GetUserNameByUserId(int userId); GetProductInformationByProductId(int productId); public void abc(int i) { System.Console.WriteLine("abc" + i); } public void abc(string i) { Syste

我在项目中使用Windows Communication Services(WCF)

在我的项目中

我编写的函数如下所示:

GetUserNameByUserId(int userId);
GetProductInformationByProductId(int productId);
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);
}
但是这个命名已经变得越来越复杂了

例如,我有5个参数要传递给函数,在这种情况下,函数名如下:

GetStackOverFlowByStackByOverByFlowByIdByStackOverFlow(string stack, string over, string flow, int id, string stackOverFlow);
GetStackOverFlowByIdByStackOverFlow(int id, string stackOverFlow);
假设我想得到两个参数,比如blow:

GetStackOverFlowByStackByOverByFlowByIdByStackOverFlow(string stack, string over, string flow, int id, string stackOverFlow);
GetStackOverFlowByIdByStackOverFlow(int id, string stackOverFlow);
我想使用函数重载,如下所示:

GetUserNameByUserId(int userId);
GetProductInformationByProductId(int productId);
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);
.
.
不是吗? 有什么方法吗? 还是我做得对

我研究发现:

他说

但我不喜欢它,因为它有时会导致混乱

还有别的办法吗?
谢谢。

你做得很好,这就是你在WCF中重载方法的方式(使用name属性)。我看不到比使用name属性重载方法更好的方法。

你做得很好,这就是你在WCF中重载方法的方式(使用name属性)。我看不到比使用name属性重载方法更好的方法。

可以使用类作为参数

[DataContract]
public class MySearchSettings
{

    [DataMember]
    public int? ID { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string StackOver { get; set; }

}
然后创建如下方法:

public GetStackOverflowResponse GetStackOverflow(MySearchSettings searchSettings)
{
    var response = new GetStackOverflowResponse();
    try
    {
        User user = null;
        if (searchSettings == null)
            throw new ArgumentNullException("searchSettings");
        if (searchSettings.ID.HasValue)
            user = //queryByID;
        else if (!String.IsNullOrEmpty(searchSettings.Name))
            user = //queryByName;
        else if (!String.IsNullOrEmpty(searchSettings.StackOver))
            user = //queryByStackOver;
        response.User = user;
    }
    catch(Exception e)
    {
        response.ErrorMessage = String.Format("{0}: {1}",
                                              e.GetType().Name,
                                              e.Message);
    }
    return response;
}
我没有包括
GetStackOverflowResponse类
,但是您已经了解了它。
这样做的好处之一是,在部署较新版本的服务时,您可以轻松地扩展类,而不会破坏客户端的功能。

您可以使用类作为参数

[DataContract]
public class MySearchSettings
{

    [DataMember]
    public int? ID { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string StackOver { get; set; }

}
然后创建如下方法:

public GetStackOverflowResponse GetStackOverflow(MySearchSettings searchSettings)
{
    var response = new GetStackOverflowResponse();
    try
    {
        User user = null;
        if (searchSettings == null)
            throw new ArgumentNullException("searchSettings");
        if (searchSettings.ID.HasValue)
            user = //queryByID;
        else if (!String.IsNullOrEmpty(searchSettings.Name))
            user = //queryByName;
        else if (!String.IsNullOrEmpty(searchSettings.StackOver))
            user = //queryByStackOver;
        response.User = user;
    }
    catch(Exception e)
    {
        response.ErrorMessage = String.Format("{0}: {1}",
                                              e.GetType().Name,
                                              e.Message);
    }
    return response;
}
我没有包括
GetStackOverflowResponse类
,但是您已经了解了它。
这样做的好处之一是,在部署较新版本的服务时,您可以轻松地扩展类而不中断客户端的功能。

为什么要编写按id和名称过滤的函数
GetStackOverFlow(id,name)
?我假设“id”只返回一个对象。你还能过滤多少呢?很好的方法,但不要认为只有id有时一个id可以有多行,所以我必须使用更多的过滤器。为什么你要编写函数
GetStackOverFlow(id,name)
按id和名称过滤?我假设“id”只返回一个对象。你还能过滤多少?很好的方法,但不要认为只有一个id有时一个id可以有多行,所以我必须使用更多的过滤器..是的,有一些查询对象可能是我应该走的路。是的,有一些查询对象可能是我应该走的路。