views:

392

answers:

2

C# ADO.Net TableAdapter objects does not implement and interface nor a base class (other than Component).

Anyone used TableAdapter in a Template like (GoF-) pattern?

Update: I would like to solve the problem described here:
Help to improve a migration program
by using template (GoF), adapter (GoF) or other nice patterns.

+1  A: 

Like much of the BCL, as you don't have access to the internals, you'll need to define your own class hierarchy and make references to the BCL classes less directly.

Something like the Adapter pattern might suit your needs: use this to "wrap" the TableAdapter in something that you can then use as a template.

Jeremy McGee
@JeremyMcGee: Thanks +1.
Kb
+3  A: 

TableAdapters do not have concrete base class or interface. But the MS guys are smart enough to leave partial. So you can play with the TableAdapter with the partial class. We ran into similar problem where we wanted to write generic code that can address all table-adapters in the data-model. we did it as following.

1.) Defined an interface ITableAdapter

public interface ITableAdapter<TDataTable> : IDisposable
    where TDataTable : DataTable
{
    void AttachTransaction(SqlTransaction _transaction);
    SqlTransaction CreateTransaction();

    int Update(TDataTable _dataTable);

    TDataTable GetData();
    TDataTable GetById(int Id);
}

2.) Later we created partial classes for each table-adapter in the project and implemented this interface for them.

public partial class UsersTableAdapter : ITableAdapter<FileParkDataSet.UsersDataTable>
{
    #region ITableAdapter<UsersDataTable> Members

    public void AttachTransaction(SqlTransaction _transaction)
    {
        if (this.Adapter == null)
            this.InitAdapter();

        this.Adapter.InsertCommand.Transaction = _transaction;
        this.Adapter.UpdateCommand.Transaction = _transaction;
        this.Adapter.DeleteCommand.Transaction = _transaction;

        foreach (var _cmd in this.CommandCollection)
        {
            _cmd.Transaction = _transaction;
        }
    }

    public SqlTransaction CreateTransaction()
    {
        if (this.Connection.State != ConnectionState.Closed)
            this.Connection.Close();
        this.Connection.Open();

        return this.Connection.BeginTransaction();
    }

    #endregion
}

Now you can program against ITableAdapter.

this. __curious_geek
Hey, it's partial? That's great!
Jeremy McGee
Yeah - I cam imagine your joy. We struggled a lot to find a way around to work this out. And finally we found TableAdapters to be partial.
this. __curious_geek
@this.__curious_geek: Nice! +1
Kb