Generics for TableAdapters problems

I dont know if this is the right forum to post but here i go.

Situation: I have a simple database with several tables, for most of them all i have to do is a SELECT * or its UPDATE, DELETE, INSERT corresponding to its Stored Procedure. Now, for this i have one class (DataAccess.cs) that has 2 methods, one to call the GetData() of the tableadapter and another to call the Update(). They both do the same to every single table adapter, the only changes is the TableAdapter and Data table which is basically a Type difference.

So with this escenario, since the diference is only about the Type between having to GetData() or Update() i decide to use generics. But then i found lots of obstacles.

- To be able to call a method from a tableadapter using generics i need to have a base class for all tableadapters that has virtual methods for the GetData and for the Update, which does not exists. So i made my own dummy class (GenericsTableAdapter.cs which i made it inherit from IComponents for obvious resons) which only has an empty GetData() and Update() method, so my generic clas goes like this:

publicclassDataAccess <T, U>where T:DataTable,new()where U:GenericsTableAdapter,new()

{

staticpublicDataTable GetData()

{

DataTable dt =newDataTable();

U ta =new U();

ta.Connection.Open();

dt = ta.GetData();

ta.Connection.Close();

return dt;

}

staticpublicbool Update(DataTable dt)

{

bool succeed =false;

try

{

T dtT =new T();

dtT.Merge(dt);

U ta =new U();

ta.Connection.Open();

ta.Update(dtT);

ta.Connection.Close();

succeed =true;

}

catch (Exception exc)

{

}

return succeed;

}

So what was left is to change in the designer generated code for the tableadapter clases to inherit from GenericsTableAdapter and to change the GetData() and Update() methods so they override the methods on my parent class. And it worked.

Now here is the problem:

Every time i add a new tableadapter to my dataset the designer code resets, all of it. So if i had 11 tableadapters inherithing from GenericsTableAdapter and add a new one i would have to make the changes all over again to each one of them.

So, my question is, is there a way so the designer code doesn't resets each time i add a tableadapter? or is there actually a TableAdapter parent class which i could use instead for my generics?

A work arround i came up for this was to make new methods on my parent class that do the same as then GetData() and the Update() and then override them in a partial class for each tableadapter so it wouldn't matter if i added another tableadapter, it wouldnt reset my methods at least. But i felt this was not right, i would end up making extra methods that do the same as other existing methods having lots of extra code and harder to read for other people.

[4922 byte] By [Sandust] at [2007-12-23]

.NET Development

Site Classified