If chained - how to eliminate?

Asked

Viewed 517 times

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 ?

  • put the code example

  • 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?

  • I was just an example , but I still got the code

1 answer

1

I didn’t quite understand the point of the method Codigo class Negocio, but I imagine that the one that wants 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;
    }
}

You just need to pass all the variables derivada per parameter for the method RetornaValor.

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’d need to put all the variables on the list.


Since your code isn’t very clear, you may have to make some tweaks to get where you want to go.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.