I am implementing a client of a service that provides me certain classes through a WSDL. In itself these classes build a structure a bit complex, but connecting to 2 service methods the answers are almost identical in terms of objects, with the difference that for some reason the classes have different names. Setting an example:
partial class PelotasServicio1 : object{
List<Pelota1> pelotas;
}
partial class Pelota1 : object {
string color;
float diametro;
}
partial class PelotasServicio2 : object {
List<Pelota2> pelotas;
}
partial class Pelota2 : object {
string color;
float diametro;
}
What I would like to do is a class, which, for example, connects to any of the 2 services, and calculate the sum of the volumes of the balls that were received in response. (Important: I can not modify the proxy classes generated by the WSDL since they are constantly updated)
I still do not know how to implement the architecture following SOLID / DRY / KISS so as not to complicate the models.
What I came up with is to make for example a Ball or Sphere class that implements CalcularVolumen()
with the diameter it has, but I do not know how to fit this with the other classes. I could make a class that contains a CalcularVolumen(int radio)
method and already, but the problem I have in reality is that the structure has a lot of complexity.
The question is: How can I do so that the response of both methods of the service (with the possibility of adding more methods) can perform the same calculation without repeating code? As much as possible a generic solution because I was presented with many of these cases and the code is repeated. Thank you very much
Update 1: I can not modify the classes I showed, they are self-generated. Example of duplicate code I want to avoid:
// Suma los volúmenes de las pelotas del servicio 1
public double ConsumirServicio1() {
PelotasServicio1 servicio1Response = _ws.GetServicio1();
double suma = 0;
foreach (Pelota1 pelota in servicio1Response.pelotas) {
suma += (3/4)*Math.PI*Math.Pow((pelota.diametro / 2), 3);
}
return suma;
}
// Suma los volúmenes de las pelotas del servicio 2
public double ConsumirServicio2() {
PelotasServicio2 servicio2Response = _ws.GetServicio2();
double suma = 0;
foreach (Pelota2 pelota in servicio2Response.pelotas) {
suma += (3/4)*Math.PI*Math.Pow((pelota.diametro / 2), 3);
}
return suma;
}