Project type: C# Class Library being called from an ASP.NET application Framework version: 4.5.1
I've been searching all over about partial classes, and all references I have found is that they are a code smell but all of them I have found are talking about god classes which this is not. I need help deciding what the best approach would be for this scenario.
I know partial classes are generally for designer generated code but... I can't decide between the options listed below. Is my option 2 with a partial class a really bad idea?
I am writing a program to create atypical CSV files meaning the file does not contain columnar data. Each line contains different data and a varying number of columns.
Whichever option used each method needs access to two datasets and a Document
class which contains a public List<string>
which each method adds a line (string) to.
What I do know:
Each line has a non-unique type identifier such as "H00","H10","D50","D55"...
Each line has fixed number of columns based on the line type identifier Some lines are related to others while others are not.
There are about 25 (give or take 5) types of lines, so there would be about 25 files part of the partial class plus one for a constructor. This number could grow but not more than 40 at absolute most.
What I do not want:
One big class with a method for each line type.
One big method with a bunch of helper methods.
Option 1: Create a separate Class for each line type.
Option 1 problem: This creates lots of class instances.
Option 2: Create a partial class named "Line" with a separate *.cs file for each line type containing a method matching the line type it is creating. With this example, I think I would use class level properties for the datasets and document initialized in a constructor.
Option 2: Partial classes are considered a code smell because they are difficult to manage and make code more complex.
Typical method, left out parameters to keep this example simple:
public class Document
{
//The document class would not be part of the partial class
//The document has other properties, I left them out to keep it simple
public List<string> Lines { get; set; }
public Document()
{
this.Lines = new List<string>
}
}
partial class LineMethods // file named: H01.cs
{
public void H01(DataSet ds1, DataSet ds2, Document doc)
{
string[] fields = new string[3];
fields[0] = "855";
fields[1] = "value from datatable";
fields[2] = "value from datatable";
string line = string.Join(",", fields);
doc.Lines.Add(line);
}
}
partial class LineMethods // File named: H10
{
public void H10(DataSet ds1, DataSet ds2, Document doc)
{
string[] fields = new string[27];
fields[0] = "855";
fields[10] = "other from datatable";
fields[16] = "other data";
//Line variable would be added to the "document.Lines.Add"
string line = string.Join(",", fields);
doc.Lines.Add(line);
}
}
EDIT After Answer selected: @Hristo, Every line is required to execute for each file and some methods will require different parameters. The requirement of every line being created will change.
The decision on if a line is required depends on data in one of the datasets. For example, when I create invoices, if the invoice does not have a discount then I would not create a line for a discount or if an order has a shipping charge or not would determine if a line for a shipping charge would be written. I think it would be easiest to write and maintain if this logic was decided in the method creating the line. If the line is not needed then a zero length would would be returned and the line would then not be added to the Doc.Lines