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)
}
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
CreateDateTimefield 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.