3
I’m doing an application where I have the following scenario:
I have several rules (business classes)
where they all return the client code. These are separate classes that will look for the trial and error code, if you find the client code it returns and so on.
How can I use a rule without using a bunch of Ifs or Ifs chained in the class that calls the others that contains the specific business rules?
For the specific classes, I used the Pattern Strategy design.
EX: Main Class
public abstract class Geral
{
public abstract string retornaCodigo(Arquivo cliente)
{
var codigo = ""; // logica
return codigo;
}
}
//Classe derivada 1
public class derivada1 : Geral
{
public override string retornaCodigo(Arquivo cliente)
{
var codigo = ""; // logica
return codigo;
}
}
//Classe derivada 2
public class derivada2 : Geral
{
public override string retornaCodigo(Arquivo cliente)
{
var codigo = ""; // logica 2
return codigo;
}
}
//Classe derivada 3
public class derivada3 : Geral
{
public override string retornaCodigo(Arquivo cliente)
{
var codigo = ""; // logica 3
return codigo ;
}
}
//Classe de Negocio
public class Negocio
{
public string Codigo()
{
var arquivo = new Arquivo();
var derivada1 = new derivada1().retornaCodigo(arquivo);
var derivada2 = new derivada2().retornaCodigo(arquivo);
var derivada3 = new derivada3().retornaCodigo(arquivo);
if (derivada1.Equals(null))
{
return derivada1;
}
if (derivada2.Equals(null))
{
return derivada2;
}
if (derivada3.Equals(null))
{
return derivada3;
}
return "";
}
}
what I wanted and that I did not have to use Ifs in the Business class for validation whether or not found the code where it can fall in any condition gave example of 3 classes more I have more than 15 conditions ,and can increase ,in case would be many Ifs.
could display your code ?
– Marcos Brinner
put the code example
– Leonardo
The code presented does not make much sense and has many errors, has as you present a more concrete example and explain better your goal?
– Leandro Angelo
I was just an example , but I still got the code
– Leonardo