Proper way to manipulate the data received from the server

0

I send something, a username for example writer.WriteLine(Username.text);

And after handling the data on the server, I return something like:

stwSend.WriteLine("a2|Cadastro efetuado com sucesso!");

And then on the client side, I wait with a conditional

if(data.Substring(0, 2) == "a2")
 {
     var result = data.Substring(3);
 }

I've been doing this a lot in the last few days, so I wonder if there is a proper way to do this.

    
asked by anonymous 04.02.2018 / 03:37

1 answer

1

If you are building a Host service that will exchange messages with your clients , you should ideally use the already established standards for this type of operation, such as XML or JSON , you might think to take advantage of the features of the framework and provide this interaction as a SOAP , WCF or a Web API REST

Not that it's impossible to implement the way you presented it, but instead of substring() I'd make a split() by | to split the arguments. Making this a rule of your contract template.

  

{string code} | {string message}

var mensagem = data.split('|'); 
if(mensagem[0] == "a2") //E demais validações
{
   var result = mensagem[1];
}

As you are, you will soon have problems if you have to respond to a "a10" code or if you need to increase the set of information sent or received as other parameters besides the code and message. This practice will still bring you more problems if you plan to deploy your service so that third parties can make their own implementations to consume it.

    
04.02.2018 / 23:20