I will try to be as explicit as possible, in case there is a better solution to my problem than the answer to my question.
I work in C #.
I have a report template that can include any number of “functions”. A feature may be an information table, pie / bar chart, list, etc. I will generate the report as a text file or PDF (possibly other options in the future).
Until now, I have an interface IFeature, and some implementing its functions ChartFeature, ListFeatureetc. I read the list of functions included from the database and passed them to the method along with the data identifier, and the method returns the filled one of the IFeaturedesired type.
I also have an interface IReportWriterthat implements TextReportWriterPdfReportWriter. This interface has a method: AddFeature(IFeature).
The problem is what AddFeatureeach writer looks like:
public void AddFeature(IFeature)
{
InsertSectionBreakIfNeeded();
if(IFeature is TableFeature)
{
TableFeature tf = (TableFeature)feature;
streamWriter.WriteLine(tf.Title);
for(int row=0; row < tf.Data.First.Length; row++)
{
for(int column=0; i < tf.Data.Length; i++)
{
if(i != 0)
{
streamWriter.Write("|");
}
streamWriter.Write(feature.Data[column][row]);
}
}
}
else if(IFeature is ListFeature)
{
ListFeature lf = (ListFeature)feature;
streamWriter.Write(lf.Title + ": ");
bool first = true;
foreach(var v in lf.Data)
{
if(!first)
{
streamWriter.Write(", ");
}
else
{
first = false;
}
streamWriter.Write(v);
}
}
...
else
{
throw new NotImplementedException();
}
sectionBreakNeeded = true;
}
In a PDF writer, the above will be modified to create PDF table cells, text fields, etc.
It seems ugly. I like it a little better than AddFeature(ListFeature){...}, AddFeature(ChartFeature)because at least then it compiles the time, but in practice it just moves the problem, so now it's outside if I call IReportWriter if(feature is ...).
Moving the display code into a function simply eliminates the problem, because it will need to know if it should write plain text or PDF.
Any suggestions, or is it best for me to use what I have and ignore my feelings?
:
, , . , .