tags:

views:

94

answers:

2

In my abstract class (C# 3) I'm using a virtual method named GetData() which returns a DataTable. The algorithm of the method is as following:

  1. Get SQL query string from the class.
  2. Get DataTable from database using above query.
  3. Do transformations on DataTable and return it.

In the 3rd point, I clone the original DataTable in order to change the Type of column (I can't do that on populated table and can't setup this at the 2nd point) and enumerate every row in which I copy and transform data from the original one. Transformations depends on the class, every one has private methods which transforms data on its own.

The question is: How to make a method in a base class which will be based on the following three params: column name which will be converted, Type of new column and action during the transformation. Two first are quite simple but I'm not sure about the third one. I thought about designing a new class which will store these three parameters, and the action will be stored as following delegate:

public delegate object Convert(object objectToConvert);

The conversion would look something like that:

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;
}

For now, I have to override the method GetData() in every class I want to have a transformations which causes a large duplication of code. The point I want to achieve is to make a base class which will be based on params I mentioned above. Is the above solution good for this problem or is it any other way to do that? Thanks in advance.

A: 

First I'm not a big fan of DataTable, you can use DataReader instead if you do a foreach.

using (var dataReader = ....)
    {
         while(dataReader.Read())
         {
             foreach(var item in Params)
             {
                  row[item.ColumnIndex] = item.Convert(originalDataTable.Rows[rowCounter].ItemArray[item.ColumnIndex]);
             }
         }
    }

Second, make a base class with a abstract method with some code and make the object in the list Params inherits of this class and override the method only when it necessary.

public class MyClass
{
      public abstract object Convert(DataRow row)
      {
      ....
      }
}

public class foo : MyClass
{
}
Jean-Christophe Fortin
AD 1. Why not DataTable? I cannot see objections about its performance. I've got a DataTable in the result of querying SQL, so I also don't see the point to convert DataTable into DataReader. Or am I wrong? AD 2. I don't need converter for row, but for the whole column. Many classes uses the same converters (<== many classes shares the same columns) so there is no point to write them separately in classes. All I need is to not to invoke converters or implement them in child classes.
ventr1s
DataTable load all the data. DataReader will only load the current row and clean it after and you don't convert a dataTable in a dataReader you use the query (sqlcommande or something else) to initialize the dataReader. (you will only see difference when you will use big query returning like 1.000.000). For the second point is my mistake. I misunderstood.
Jean-Christophe Fortin
+1  A: 

Okay, for now I have a following solution which does fully satisfy me:

Every convertor implements following interface:

public interface IConvertor
{
 object Convert(object item);
}

Params class is as following:

Type ColumnType { get; protected set; } // New type
string[] ColumnNames { get; protected set; }
IConvertor Convertor { get; protected set; }

Every object have its own Params array property. I've got one method for every derived class and all what I have to do is to set up parameters.

Thanks for feedback and if anyone see better solution, please add it.

ventr1s