Search code examples
c#wpfinterfacedatasetdataadapter

Create interface for custom Adapter and DataTable


I am learning to use interfaces and there is a problem when using the generated dataset. I created two similar tables in the database, then created two data adapters in the dataset based on these tables. Then I created classes that will describe the adapter, datable and necessary methods.

My DataSet

class DB
{
    public class Table1 {
        public DataSetTableAdapters.Table1TableAdapter adapter;
        public DataSet.Table1DataTable dataTable;

        public void Init()
        {
            adapter = new DataSetTableAdapters.Table1TableAdapter();
            dataTable = new DataSet.Table1DataTable();
        }
    }

    public class Table2
    {
        public DataSetTableAdapters.Table2TableAdapter adapter;
        public DataSet.Table2DataTable dataTable;

        public void Init()
        {
            adapter = new DataSetTableAdapters.Table2TableAdapter();
            dataTable = new DataSet.Table2DataTable();
        }
    }
}

I tried to implement, for example, like this

interface ISecondaryTable
{
    DataTable dataTable { get; set; }
    IDataAdapter adapter { get; set; }
}

And I tried many other options. But without results. Does anyone know their common class?


Solution

  • If you open the Object Browser in visual studio (Ctrl+Alt+J in 2017, used to be Ctrl-W,J as a chord) you can see more info about the tableadapters in your project:

    enter image description here

    All TableAdapters inherit from Component. They "HAVE-A" DataAdapter, they are not "IS-A" DataAdapter. For example, they look like this:

    public class XTableAdapter: Component{
    
      private DataAdapter _da;
    
    }
    

    They do not look like this:

    public class XTableAdapter: DataAdapter
    

    All this said, I'm not sure why you want to treat them this way or encapsualte them along with the data table. Data is stored in the datatable, tableadapters push it between db and datatable. I don't think i've ever seen someone do, for example, a class that wraps a StreamWriter (thing that writes a file) and a String (the content of the file):

    class CombinedFileContentAndWriter{
    
      StreamWriter sw = newStreamWriter(@"C:\temp\x.txt");
    
      string content = "Hello World";
    
      void DoIt(){
        sw.Write(content);
      }
    }
    

    It's not to say you can't, it's just weird. TableAdapters are supposed to be short-to-medium life things that are called upon to move data; they don't need pairing up inseparably from that data. One tableadapter can readwrite hundreds of different instances of a datatable. TableAdapters can be created and thrown away on demand and they don't need to remain paired with the data they downloaded in order to function. You can:

    var dt = new XTableAdapter().GetDataByName();
    
    //manipulate dt in a 30 minute operation
    
    new XTableAdapter().Update(dt); //a different tableadapter sends the data back to the DB
    

    There's not much point trying to find a generic way to refer to tableadapters, becawuse they are all customized exactly to a specific datatable, and have names of methods that are variable, and hence not overloadable:

    SchoolDataset ds = new SchoolDataSet();
    new StudentTableAdapter.FillByStudentId(ds, 123);
    new StaffTableAdapter.FillByStaffLastName(ds, "Smith");
    new ClassroomTableAdapter.FillByYearDesignation(ds, "First grade");
    

    This is about as deep as you need to go with tableadapters and typed datatables/sets.

    The question Karen linked to is a smart resource, for sure, but it's worth noting that the lead answer chose to create a generic way to address tableadapters so they could easily enroll each adapter in a transaction, whereas Microsoft intended that to enroll tableadapter operations in a transaction they should be executed inside a TransactionScope