c# - Need advice for refactoring a large class -


our c# application produces reports of various types. each type of report has xml schema defining options , features available, user can create xml document describing report want. there elements common multiple report types, , there interdependencies among elements.

we have single class full of static methods handle parsing of xml. take schema-validated document, , return object representing type of report , configured options. class looks this:

public class reportfactory {     //let's 4 types of reports     public static reporttype1 createreporttype1(xdocument document)     {         //logic create type of report, calling private methods in class     }     ....      public static reporttypen createreporttypen(xdocument document)     {         //logic create type of report     }       //several dozen private methods, called public methods or each other     private static feature1 createfeature1(xelement element)     {         //create feature of report      }      private static featuren createfeaturen(xelement element, featurem m)     {         //create feature relies on built feature     }      private static featurex createfeaturex(reporttype2 report, xelement elementforx, xelement elementrelatedtox)     {         //create feature, relies on          //more 1 element and/or partially built report of given type     }      private static void updatefeaturenwithstufffromfeaturem(featuren n, featurem m)     {         //modify parts of built report, based on other parts of report      }     ... } 

i believe intention encapsulate details of xml structure, suppose does. doesn't suffer duplicated code. large , hard read, , gets worse add more features. it's difficult test, since relies heavily on things being done in correct order.

i'd refactor it, far thing can think of split multiple classes; say, class each report type , additional helper class common stuff. still messy , possibly harder read. there way organize this? patterns might help? i've looked @ bunch of creation patterns , haven't found seems fit.

update: sorry have not had time or budget work on piece of refactoring, suggestions. more think it, more (something like) chain of responsibility idea. starting point (public function) create return object , fill in basic stuff, hand off object , xml next piece. each piece know parts of object , xml needs, , each piece can tested independently looking @ changes object.

i guess you're going need several patterns , principles enhance class(es). using factory pattern. problem seem describing though handling specific order of activities , breaking classes down smaller chunks.

in order handle ordering of activities, looks use chain of responsibility pattern..

on note of testability of "portions" of solution because of dependencies, part of test setup (you need better mocks or stubs support activities of method) , perhaps better design come out of efforts test application.


Comments