Tuesday, 27 August 2013

Guidance with refactoring issue

Guidance with refactoring issue

I have a couple of methods, 10 to be exact. All of them have the same
return type and accept the same params like so
public string GenerateDataForABC(List modelList, Student student)
Except for some customization they all pretty much do the same thing,
i.e., return a string.
For example the method GenerateDataForDEF will have a unique code like so:
StringBuilder sb = new StringBuilder();
sb.Append("STUDENT_NUMBER");
sb.Append("\tCALLBACK_NUMBER");
sb.Append("\tSPECIALINSTRUCTIONS");
...
So that one creates a heading for a tab separated file. And then there
will be a for loop that will have code like so:
foreach (Model model in modelList)
{
string sstudentNumber = "789";
sb.Append("\t");
sb.Append("\t");//SPECIALINSTRUCTIONS
sb.Append("\t" + model.ExamNumber);
...
}
The foreach is common in all the methods the only difference being the
properties used from the Model object. So one method may use all the
properties, the other might use only 5.
Currently, as mentioned in the begining, there are seperate methods and
the properites are used as per those methods requirement.
As I see it there are two problems to solve here:
The custom sections in each method. How to handle that?
The foreach. Is there an alternate?
I tried putting the required properties of the model in an xml and then to
build the model for each method assigning only the required properties.
That seems to work but solves only one part of the puzzle.
Any clue or direction is appreciated.
Regards.

No comments:

Post a Comment