mercredi 26 décembre 2018

Using Chain Of Responsibility Pattern

I was checking the following thread: Refactoring if-else if - else

Im seeing on the proyect Im working a lot of if with if else and else statements nested with each other and I wanted to take opportunity on this pattern but I wanted to check if this piece of code can be refactored like this.

So the code in question is the following:

            if (factura.fac_diferencia > 0)
            {
                if (factura.fac_diferencia == 1)
                {
                    throw new Exception("Error 1");
                }
                else if (factura.fac_diferencia == 2)
                {
                    throw new Exception("Error 2");
                }
                else if (factura.fac_diferencia == 3)
                {
                    throw new Exception("Error 3");
                }
                else if (factura.fac_diferencia == 4)
                {
                    throw new Exception("Error 4");
                }
                else
                {
                    throw new Exception("Another Error");
                }
            }

What I did is move the error codes in a constant class where I can manage this particular error codes and I moved the strings to a resx file they already had.

Now the code is like this:

                            ValidacionAprobacionFactura ValidaImporteDesglozado = new ImporteTotalDesTolerancia();
                            ValidacionAprobacionFactura PercepcionSuperaTolerancia = new PercSuperaTolerancia();
                            ValidacionAprobacionFactura DifTotalDesglozadoTranSuperaTolerancia = new DifTotalesDesgTranSuperaTolerancia();
                            ValidacionAprobacionFactura DifTotalTranSuperaTolerancia = new DifTotalesTranSuperaTolerancia();
                            ValidacionAprobacionFactura NoHayCoherenciaTotales = new NoCoherenciaTranFac();

                            ValidaImporteDesglozado.SiguienteSucesor(PercepcionSuperaTolerancia);
                            PercepcionSuperaTolerancia.SiguienteSucesor(DifTotalDesglozadoTranSuperaTolerancia);
                            DifTotalDesglozadoTranSuperaTolerancia.SiguienteSucesor(DifTotalTranSuperaTolerancia);
                            DifTotalTranSuperaTolerancia.SiguienteSucesor(NoHayCoherenciaTotales);

                            if (factura.fac_diferencia > Constantes.CODIGOERRORDEFAULT)
                            {
                                string resultado = ValidaImporteDesglozado.Ejecutar(factura.fac_diferencia);
                                throw new Exception(resultado);
                            }

I did not include the pattern in question as you may already know it.

I wanted to ask if this is the correct way to refactor this code as, this throws an exception. Maybe there is another type of Pattern for Bubbling an error message?

Another thing would be, for each type of error, I will need to instantiate a new ValidacionAprobacionFactura. Is there any way to accomplish the same maybe with one new? a list of this type maybe?.

Aucun commentaire:

Enregistrer un commentaire