c# - 在 WCF 操作契约中传递原始变量。优点和缺点?

标签 c# .net wcf web-services

我创建了一个简单的 WCF 服务。我正在编写一种方法来根据某些搜索条件搜索特定实体。

[OperationContract]
List<SiteDTO> GetList(int? siteID, string code, string name, 
    string notes, byte? status, string description, 
    int? modifiedBy, DateTime? modifiedDate, long? 
    timeStamp, int? pageNo, int? pageSize, out int? 
    totalRows, int x);

我有两个问题:

  1. 我应该将原始变量传递给服务方法还是应该将它们全部包装在一个类中(即 SiteSearchDTO)。以及为什么?请详细说明。

  2. 我的第二个问题是,当我在项目中添加对服务的引用时,我得到了在那里生成的相应方法。但在 Reference.cs 中具有不同的签名。

public System.Collections.Generic.List<RPMS.Web.SiteService.SiteDTO>
    GetList(out System.Nullable<int> totalRows, 
    System.Nullable<int> siteID, string code, string name, 
    string notes, System.Nullable<byte> status, string description, 
    System.Nullable<int> modifiedBy, 
    System.Nullable<System.DateTime> modifiedDate, 
    System.Nullable<long> timeStamp, 
    System.Nullable<int> pageNo, 
    System.Nullable<int> pageSize, int x)

问题是生成的方法有int? TotalRows 作为第一个参数,但在原始服务方法中,totalRows 是倒数第二个变量。 为什么?

最佳答案

回答你的第一个问题,there's a number of differing opinions但是,我认为the one from Robert Martin is the best :

The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification -- and then shouldn't be used anyway.

要回答为什么,其实很简单。您没有encapsulation拥有所有这些参数。只要看一下列表,很明显除了 pageNopageSizex 之外的所有内容都是相关的,因此它们应该封装在一个类中/结构来反射(reflect)它们彼此相关,即使只是作为一个分组。

假设这些都放入一个类型中,那么您就拥有了一个具有良好封装性的单个参数函数,这使得整体管理更加容易。

对于你的第二个问题,我怀疑你的代理和生成它的服务/方法不同步。 svcutil.exe tool (生成您的代理)尊重参数的顺序。如果您遇到了不同步的情况(意味着您验证了代理代码和服务器代码没有不同步),那么您就发现了一个错误(但我会首先通过重新生成代理来仔细检查)。

关于c# - 在 WCF 操作契约中传递原始变量。优点和缺点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11177549/

相关文章:

c# - XML 命名空间让我感到困惑

c# - 如何递归遍历嵌套泛型集合?

asp.net - 如何在 WCF 和 ASP.NET 之间集成企业库验证应用程序 block ValidationResults?

asp.net - WPF WCF MVVM 内存不足异常

c# - 我是否需要在 WCF DataContract 中公开一个构造函数,以便它在客户端的对象实例化期间工作?

c# - 将 SafeHandles 从非托管编码到托管

c# - Visual C# 2010 速成版 : Same Form But Different Size on Win7 and WinXP

c# - 尽管计算在不同的任务中运行,但主 UI 窗口卡住

.net - 与 ConfigurationProperty 属性一起使用时,属性类型的隐式约定是什么?

c# - 查看一组实数的快速方法