In an abstract class ( C # 3 ), there is a virtual method with a name GetDatathat returns a DataTable. The algorithm of the method is as follows:
- Get the SQL query string from the class.
- Get a DataTable from the database using the above query.
- Make conversions to a DataTable and return it.
In the third paragraph, I clone the original DataTable to change the type of the column (I can’t do this according to the completed table and cannot set it at the 2nd point) and list each row in which I copy and convert the data from the original. Transformations depend on the class, each of them has private methods that transform the data themselves.
The question arises: how to create a method in a base class that will be based on the following three parameters: the name of the column to be converted, the type of the new column, and the action during the conversion. The first two are pretty simple, but I'm not sure about the third. I was thinking of developing a new class that will save these three parameters, and the action will be saved as the following delegate:
public delegate object Convert(object objectToConvert);
The conversion will look something like this:
int rowCounter = 0;
foreach(DataRow row in dt.Rows)
{
foreach(var item in Params)
{
row[item.ColumnIndex] = item.Convert(originalDataTable.Rows[rowCounter].ItemArray[item.ColumnIndex]);
}
++rowCounter;
}
Currently, I have to override the method GetData()in each class, which I want to have conversions that lead to a lot of code duplication. What I want to achieve is to make a base class that will be based on the parameters mentioned above. Is the above solution good for this problem or is this any other way to do this?