1
votes

I am trying to pass separate typed dataset data tables to the same method. The datatables are similar, but distinct. The method performs the same function on the datatables. Instead of essentially duplicating the method and changing the parameter type depending on the datatable type, is there a way to do something along the lines of creating a custom interface for the datatables, that way I can have a single method with a parameter of the interface type?

what I currently have:

private PassdownDataset.SalesOrdersForManagerDataTable datatable1;
private PassdownDataset.SalesOrdersDataTable datatable2;

public SomePublicMethod()
{
PopulateDepartmentName(datatable1);
PopulateDepartmentName(datatable2);
}

private void PopulateDepartmentName(PassdownDataset.SalesOrdersForManagerDataTable dt)
{
//(operations performed on datatable)
}

private void PopulateDepartmentName(PassdownDataset.SalesOrdersDataTable dt)
{
//(operations performed on datatable)
}

what I would like to have:

private PassdownDataset.SalesOrdersForManagerDataTable datatable1;
private PassdownDataset.SalesOrdersDataTable datatable2;

public SomePublicMethod()
{
PopulateDepartmentName(datatable1);
PopulateDepartmentName(datatable2);
}

private void PopulateDepartmentName(
               PassdownDataset.CommonInterfaceForBothDataTables dt)
{
//(operations performed on datatable)
}
1
You can either use the same DataTable(just different DataRows) or ask for DataTable or IEnumerable<DataRow> as input instead of your strongly typed DataTables or do you really need the typed DataTable functionality? - Tim Schmelter
You could hack this together using covariant interfaces. - SLaks
The only issue there is the typed DataRows are also different, and unfortunately I do need to use the typed datatable functionality - spurro
I don't know about covariant interfaces but I will be looking them up - spurro

1 Answers

0
votes

You could solve this using inheritance, I am sure. But that might turn out to be awkward and forced.

I have found the dynamic feature added in C# 4 to be quite nice for such things. The main disadvantage is that you lose static typing and refactoring-friendliness. I think that is OK if your usage of dynamic is very localizes (such as contained within a single helper method).

As an example, imagine that all database entities have a CreateDateTime field and you want helper method that initializes it. That is a nice use-case for dynamic. The alternative would be to have all entities inherit an interface.