c# – If chained – how to remove?

Question:

I'm making an application where I have the following scenario:

I have several rules (business classes)

where all return the customer code. They are separate classes that will look for the trial and error code, if it finds the client code, it returns it and stops there.

How can I use a rule without using a bunch of IFs or IFs chained together in the class that calls the others that contain the specific business rules?

For specific classes, I used the design pattern strategy.

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 didn't have to use Ifs in the Business class for validation if you found the code where it can fall in any condition or not, gave an example of 3 classes but I have more than 15 conditions, and can increase case would be many Ifs.

Answer:

I didn't quite understand the purpose of the Codigo method of the Negocio class, but I imagine that it intends to return a first value that is not null or empty.

If so, the following code can help you minimize the IF :

 public class Negocio { public string Codigo() { var arquivo = new Arquivo(); string derivada1 = new derivada1()?.retornaCodigo(arquivo); string derivada2 = new derivada2()?.retornaCodigo(arquivo); string derivada3 = new derivada3()?.retornaCodigo(arquivo); return RetornaValor(derivada1, derivada2, derivada3); } private string RetornaValor(params string[] strValores) { foreach (var strValor in strValores) { if (!string.IsNullOrEmpty(strValor)) return strValor; } return string.Empty; } }

All you need to do is pass all the derivada variables per parameter to the RetornaValor method.

Another way would be to pass a string list:

 private string RetornaValor(List<string> strValores) { foreach (var strValor in strValores) { if (string.IsNullOrEmpty(strValor)) return strValor; } return string.Empty; }

But then you would need to gather all the variables in the list.


Since your code was not very clear, you may have to tweak to get where you want.

Scroll to Top