C# Web Services SOAP – Multiple DTOs vs Single DTO vs Primitives

csoapweb services

I'm wondering what's the best approach, and its advantages, when specifying parameters for the Web Service methods. Best to explain it through examples.

In my (SOAP) WebService, used by a Xamarin mobile app, I have a WebMethod SubmitForm(int, TransactionData, List<Answer>), where:

int is the ID of the Project and specifies which Database to connect to,

TransactionData is a DTO containing data about the user and the form, and

Answer is a DTO containing the ID and answer for a single question.

Because I have separate tables for TransactionData and Answers, these are fairly unrelated to each other, but I'm considering creating a new DTO, SubmitRequest, which would contain these 3 objects. What are the advantages and disadvantages of these options, apart for readability, and a minuscule overhead of instantiating and extracting from SubmitRequest?

Another situation to consider is a WebMethod which accepts a single primitive type, like int. Would it be better to just let it accept an int, or wrap it in a DTO, that contains just a single property? Frankly, I'm not a fan of the latter, because you end up with DTO for a string, int, etc.

Hence the question, what is the most advantageous for the system? Is there even a difference? Or maybe it's just a matter of personal preference?

Best Answer

There is a few things to unpack here:

Having multiple parameters in a method

It creates a dependency on the caller of the method because it now needs knowledge about order of parameters. Having a single parameter that semantically represents what the parameter is will help decouple things a little bit more. Use your judgment to determine how many parameters you tolerate before it's "too much".

Wrapping two models in a single model

It can go wrong or it could improve the semantics throughout your code. When you have several parameters in a method it is possible that they have some kind of affair (secret relationship), they don't know what to call it so it's up to you, they either belong to one another, one has the other, they both belong to a higher level of abstraction.

Something to add here is that your models/DTOs don't have to reflect your DB schema, therefore, just because you are putting both models in a DTO doesn't mean that they now need a Foreign Key to one-another, that is only necessary if you put one of the DTOs inside of the other.

Using a primitive method as a parameter

Is semantics worth the hassle? Sometimes it is, sometimes it's not. Do you see value in naming that a bit better? Should it be maybe a property of the infamous child product of the affair of all the parameters? Maybe. But if it's just one parameter and it's primitive... I would just whatever.

Hope this helps. Let me know your thoughts.