If chained - how to delete?

2

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

I have several rules (business classes)

where all return the client code. They are separate classes that will look for the code trial and error, if find the client code returns it and so on.

How can I use a rule without using a bunch of IFs or threaded IFs in the calling class that contains 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 did not have to use Ifs in the Business class for validation whether or not I found the code where it can fall under any condition gave example of 3 classes plus I have more than 15 conditions, and can increase , in that case it would be many Ifs.

    
asked by anonymous 21.08.2018 / 17:32

1 answer

0

I did not really understand the purpose of the Codigo method of class Negocio , but I imagine that you want to return a first value that is not null or empty.

If so, the following code can help you minimize 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 variables derivada per parameter to the RetornaValor method.

Another way would be to pass a list of string :

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

    return string.Empty;
}

But then you would have to add all the variables in the list.

Since your code is not very clear, you may have to do some tweaks to get where you want.

    
31.08.2018 / 13:56