Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling external services in scala code with dependencies
    text
    copied!<p>I am facing a major issue with my design at this juncture. My method is trying to accomplish the follows:</p> <ol> <li>Insert the passed in object into the database.</li> <li>Get the autoincremented id from the insert and use it to call webservice1 along with the object.</li> <li>Get the result from webservice1 and call webservice2 with the original object and some response from webservice1.</li> <li>Combine the results from webservice1 and 2 and write it into the database.</li> <li>Get the resulting autoincremented id from the last insert and call webservice3 with the original object that would eventually result into the success or failure of the operation.</li> </ol> <p>I want to design this in a flexible manner since the requirements are in a flux and I do not want to keep on modifying my logic based on any changing. I do realize some amount of change is inevitable but I would like to minimize the damage and respect the open-closed principle.</p> <p>My initial take was as follows:</p> <pre><code>def complexOperation(someObject:T) = dbService.insertIntoDb(someObject) match { case Left(e:Exception) =&gt; Left(e) case Right(id:Int) =&gt; webService.callWebService1(id,someObject) match { case Left(e:Exception) =&gt; Left(e) case Right(r:SomeResponse1) =&gt; webService.callWebservice2(r,someObject) match { case Left(e:Exception) =&gt; webService.rollbackService1();Left(e) case Right(context:ResponseContext) =&gt; dbService.insertContextIntoDb(context) match { case Left(e:Exception) =&gt; Left(e) case Right(id:Int) =&gt; webService.callWebservice3(id,someObject) match { case Left(e:Exception) =&gt; webService.rollbackService3();Left(e) case Right(r:Response) =&gt; Right(r) } } } } </code></pre> <p>As you can see, this is a tangled mess. I can neither unit test it, nor extend it nor very easily debug it if things spiral out of control. This code serves its purpose but it will be great to get some ideas on how I should refactor it to make the lives of the people who inherit my code a little more easier.</p> <p>Thanks </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload