mercredi 23 octobre 2019

Nested Webservice Calls Design Pattern

I have an array of complex objects that I am sending over to a web service. I have to send them individually, it's just the way they have built it their end, and it currently looks a real mess because it is a bunch of nested ifs checking the responses back from the service and then taking a course of action dependent on that. Here's an example of how it looks:

var req = Requests.GetInsertServiceRequest(billingReq, userName, contractNo);

result.Add(Connector.CallWebService<Service>(req, "return", errorMessage));
  if (errorMessage.HasError) {
    if (errorMessage.Error.StartsWith("Entity not found: [GSM]")) {
      //make a call to correct this error and then try the original call again
    } elseif (errorMessage.Error.StartsWith("some other expected error")) {
      //do something else to correct the error and call the original request again
  } else //successfully called adding the service so call the service on some of the nested objects
  {
    foreach(var associatedItem in billingReq.associatedItems){
      var subReq = Requests.GetInsertAssocatedItemRequest(associatedItem, userName, contractNo);
      result.Add(Connector.CallWebService<AssociatedItem>(subReq, "return", subErrorMessage));
      if(subErrorMessage.HasError) {
        and so on

I can see that this is a terrible design. Teh method is getting really long and I know I need to break it up into more atomic methods. I just wondered if anyone had any ideas on what best pattern to use to solve handling different error messages coming back and then trying alternative requests and the initial request again, and then all of the sub requests on the objects within the parent object.

I am using a C# class library to call the service but this could be any language really.

Thanks in advance for any help.

Aucun commentaire:

Enregistrer un commentaire