将 TableAdapter 与基类、接口或部分类一起使用的更智能方法

发布于 2024-08-13 06:52:37 字数 283 浏览 2 评论 0原文

C# ADO.Net TableAdapter 对象不实现接口或基类(组件除外)。

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

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄暮涼年 2024-08-20 06:52:37

TableAdapter 没有具体的基类或接口。但 MS 的人很聪明,他们会部分离开。因此,您可以使用部分类来使用 TableAdapter。我们遇到了类似的问题,我们想要编写可以解决数据模型中所有表适配器的通用代码。我们做了如下。

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

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 进行编程。

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.

锦上情书 2024-08-20 06:52:37

与 BCL 的大部分内容一样,由于您无权访问内部结构,因此您需要定义自己的类层次结构并减少对 BCL 类的直接引用。

适配器模式 之类的东西可能会满足您的需求:用它来“包装”TableAdapter您可以将其用作模板。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文