2

I have created a simple WCF service. I am writing a method to search certain entity based on some search criteria.

[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);

I have two questions here:

  1. Should I pass the primitive variables to the service method or should I wrap them all in a class(i.e. SiteSearchDTO). and Why? Details please.

  2. My second question is when I add the reference to the service in a project, I get the corresponding method generated there. But with a different signature in 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)

The issue is the generated method has int? totalRows as the first parameter but in original service method totalRows is second to last variable. Why?

casperOne
  • 73,706
  • 19
  • 184
  • 253
Vaibhav Jain
  • 33,887
  • 46
  • 110
  • 163

2 Answers2

2

To answer your first question, there's a number of differing opinions, however, I think that 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.

To answer why, though, well, it's simple. You have no encapsulation in having all those parameters. Just looking at the list, it's obvious that everything except maybe pageNo, pageSize and x are related, so they should be encapsulated in a class/structure to reflect that they are related to each other, even if only as a grouping.

Assuming that these are all placed into a type, then you have a function with a single parameter with good encapsulation, which makes it easier to manage overall.

To your second question, I suspect that your proxy and the service/method it was generated from are out of sync. The svcutil.exe tool (which generates your proxies) respects the order of the parameters. If you have a case where it doesn't (meaning you validated that your proxy code and server code are not out of sync) then you've found a bug (but I'd double check by regenerating the proxy first).

Community
  • 1
  • 1
casperOne
  • 73,706
  • 19
  • 184
  • 253
  • 2
    To expand on the first point, imagine updating your method to add a `createdBy` option. Everywhere you'd need to update the method call just to include it. Furthermore, your SiteSearchDTO creation might be simplified/readable using a factory or fluent API. Assuming that `null` means "do not search by", a search might look like: `GetList(null, null, null, null, 0x01, null, null, DateTime.Now.AddDays(-7), null, 1, 10, null, null)` What are we searching for? Instead: `GetList(SearchBy().Status(Status.Active).ModifiedBy(DateTime.Now.AddDays(-7)).Paging(1, 10))` it becomes obvious and readable. – Chris Sinclair Jun 24 '12 at 14:10
  • @casperOne Thanks, all other parameters are also the part of a business entity and are related to each other. But the difference is, here in this method I need nullable types. I guess, I have my answer, I should create a entity SiteSearchDTO that contains all these variables and a entity for the paging attributes. – Vaibhav Jain Jun 24 '12 at 14:14
  • @vaibhav It's what I would do. It doesn't have to have any logic in it, it's just a container for all of these values (some of which are nullable). – casperOne Jun 24 '12 at 14:20
0

For the first question, it depends on your architecture and technical choice

- The problem when you pass entity, you create dependance on your entity library, and you contraign your client to reference it, your layer become depend of entity layer.



2. For the second question proxy class is generated by basing on meta data of your wsdl, check your wsdl of your web service.
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51