我创建了一个简单的 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);
我有两个问题:
我应该将原始变量传递给服务方法还是应该将它们全部包装在一个类中(即 SiteSearchDTO)。以及为什么?请详细说明。
我的第二个问题是,当我在项目中添加对服务的引用时,我得到了在那里生成的相应方法。但在
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拥有所有这些参数。只要看一下列表,很明显除了 pageNo
、pageSize
和 x
之外的所有内容都是相关的,因此它们应该封装在一个类中/结构来反射(reflect)它们彼此相关,即使只是作为一个分组。
假设这些都放入一个类型中,那么您就拥有了一个具有良好封装性的单个参数函数,这使得整体管理更加容易。
对于你的第二个问题,我怀疑你的代理和生成它的服务/方法不同步。 svcutil.exe tool (生成您的代理)尊重参数的顺序。如果您遇到了不同步的情况(意味着您验证了代理代码和服务器代码没有不同步),那么您就发现了一个错误(但我会首先通过重新生成代理来仔细检查)。
关于c# - 在 WCF 操作契约中传递原始变量。优点和缺点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11177549/