将TableAdapter与基类,接口或子类一起使用的更聪明方法 [英] Smarter ways to use TableAdapter with base-class, interface or partial class

查看:44
本文介绍了将TableAdapter与基类,接口或子类一起使用的更聪明方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#ADO.Net TableAdapter对象未实现接口或基类(除了Component).

C# ADO.Net TableAdapter objects do not implement an interface or a base class (other than Component).

有人在(GoF)模板模式的应用程序中使用过TableAdapter吗?

Has anyone used TableAdapter in an application of the (GoF) Template pattern?

更新:我想解决这里描述的问题:
帮助改进迁移程序
通过使用模板模式(GoF),适配器模式(GoF)或其他不错的模式.

Update: I would like to solve the problem described here:
Help to improve a migration program
by using the Template Pattern (GoF), Adapter Pattern (GoF) or other nice pattern.

推荐答案

TableAdapter没有具体的基类或接口.但是,MS专家足够聪明,可以只留下一部分.因此,您可以使用带有部分类的TableAdapter.我们遇到了类似的问题,我们想编写通用代码来解决数据模型中的所有表适配器.我们做到了如下.

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.)定义了一个接口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.)后来,我们为项目中的每个表适配器创建了部分类,并为它们实现了此接口.

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
}

现在您可以针对ITableAdapter进行编程了.

Now you can program against ITableAdapter.

这篇关于将TableAdapter与基类,接口或子类一起使用的更聪明方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆