强类型数据行值得吗?

发布于 2024-10-17 01:54:43 字数 96 浏览 5 评论 0原文

我有一个数据行,我传递它并使用它进行操作,并且想要对其进行强类型化,但不需要对表本身进行强类型化。

是否有一个工具可以使用 isnull 方法等自动生成强类型行?

I have a datarow that I pass around and do things with, and would like to strongly type it, but don't need to strongly-type the table itself.

Is there a tool that will autogenerate a strongly-typed row with isnull methods and such?

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

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

发布评论

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

评论(2

枉心 2024-10-24 01:54:43

我认为为 ADO 数据类型创建强类型类是值得的。有两种方法可以做到这一点:

  1. 手动编写 DataRow 的子类或封装您想要的行为的任何内容。
  2. 编写数据的 XSD 文件,并让 Visual Studio 构造强类型类。

第一种方法的优点是它提供了一个自定义 API,可以准确地公开您想要的内容。第二种方法通常更快。

In my opinion is is worthwhile to create a strongly-typed class for ADO data types. There are two ways you can do this:

  1. Hand-code a subclass of DataRow or whatever that encapsulates the behavior you want.
  2. Write an XSD file of your data and let Visual Studio construct strongly-typed classes.

The advantage of the first method is that it provides a custom API exposing exactly what you want. The second method is often faster.

别低头,皇冠会掉 2024-10-24 01:54:43

这是值得的,因为您可以使用强类型 DataRow/DataSet 进行编译时间检查。

下面的代码(只是一个示例)展示了我是如何做到的。我有一个工具可以从数据库信息生成所有类,特别是存储过程的输出。因此,我有一个存储过程,它返回一个结果集,其字段映射到下面的 PostDtw 类的属性。

该工具(带有源代码)可以在我的博客上找到。它还以类似的方式生成 DataReaders 包装器。您可以从这里获取该工具
数据访问层 CodeGen

下面的“Main”方法显示了如何使用班级。请注意,在 foreach 循环中,您如何访问类的属性,但在幕后,属性获取器正在使用 DataRow。

方法“GetPosts1”和“GetPosts2”显示了如何基本上使用 DataTable 并将其“转换”为

IEnumerable<PostDtw>

. GetPosts1 本质上使用相同的实例,而 GetPosts2 为每一行创建一个新实例。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication5
{
  class Program
  {
    static void Main(string[] args)
    {
      var posts = GetPosts1();
      foreach (var post in posts)
      {
        Console.WriteLine(post.PostId);
        Console.WriteLine(post.PostTitle);
        Console.WriteLine(post.PostSlug);
        Console.WriteLine(post.PostDate);
      }
    }

    static IEnumerable<PostDtw> GetPosts1()
    {
      DataTable postsDt = GetPostsDataTable();
      PostDtw postDtw = new PostDtw();

      foreach(DataRow row in postsDt.Rows)
      {
        postDtw.DataRow = row;
        yield return postDtw;
      }        
    }

    static IEnumerable<PostDtw> GetPosts2()
    {
      DataTable postsDt = GetPostsDataTable();
      foreach (DataRow row in postsDt.Rows)
        yield return new PostDtw(row);
    }

    static DataTable GetPostsDataTable()
    {
      throw new NotImplementedException();
    }
  }

  /// <summary>
  ///This is the Base Class for all DataTable Wrappers
  /// </summary>
  public class BaseDataTableWrapper
  {
    public DataRow DataRow { get; set; }

    public BaseDataTableWrapper()
    {
    }

    public BaseDataTableWrapper(DataRow row)
      : this()
    {
      DataRow = row;
    }
  }

  #region [GetPost]

  /// <summary>
  ///This class is a wrapper around a DataTable,
  ///Associated with the stored procedure - GetPost
  ///This class provides a strongly typed interface to access data from the DataTable
  ///containing the result of the given stored procedure.
  /// </summary>
  public sealed class PostDtw : BaseDataTableWrapper
  {
    public Int32 PostId { get { return (Int32)DataRow[0]; } set { DataRow[0] = value; } }
    public DateTime PostDate { get { return (DateTime)DataRow[1]; } set { DataRow[1] = value; } }
    public String PostSlug { get { return (String)DataRow[2]; } set { DataRow[2] = value; } }
    public Int32 UserId { get { return (Int32)DataRow[3]; } set { DataRow[3] = value; } }
    public String PostTitle { get { return (String)DataRow[4]; } set { DataRow[4] = value; } }
    public String PostText { get { return (String)DataRow[5]; } set { DataRow[5] = value; } }
    public Boolean PostIsPublished { get { return (Boolean)DataRow[6]; } set { DataRow[6] = value; } }
    public Boolean PostIsPublic { get { return (Boolean)DataRow[7]; } set { DataRow[7] = value; } }
    public String PostTitleImg { get { if (DataRow[8] != DBNull.Value) return (String)DataRow[8]; else return default(String); } set { DataRow[8] = value; } }

    public PostDtw()
      : base()
    {
    }

    public PostDtw(DataRow row)
      : base(row)
    {
    }
  }

  #endregion [GetPost]

}

It is worthwhile since you could get compile time checking with strongly typed DataRow/DataSet.

The code below (just a sample) shows how I do it. I have a tool that generates all of the classes from Database information, in particular the output of stored procedures. So I have a stored procedure that returns a result set whose fields map to the properties of the PostDtw class below.

The tool (with source) is available on my blog. It also generates DataReaders wrappers in a similar manner. You can get the tool from here
Data Access Layer CodeGen

The "Main" method below shows how you'd use the class. Notice how in the foreach loop you're accessing properties of a class but behind the scenes the property getter is using a DataRow.

The methods "GetPosts1" and "GetPosts2" shows how you'd basically use a DataTable but "convert" it to an

IEnumerable<PostDtw>

. GetPosts1 essentially uses the same instance, while GetPosts2 creates a new instance for each row.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication5
{
  class Program
  {
    static void Main(string[] args)
    {
      var posts = GetPosts1();
      foreach (var post in posts)
      {
        Console.WriteLine(post.PostId);
        Console.WriteLine(post.PostTitle);
        Console.WriteLine(post.PostSlug);
        Console.WriteLine(post.PostDate);
      }
    }

    static IEnumerable<PostDtw> GetPosts1()
    {
      DataTable postsDt = GetPostsDataTable();
      PostDtw postDtw = new PostDtw();

      foreach(DataRow row in postsDt.Rows)
      {
        postDtw.DataRow = row;
        yield return postDtw;
      }        
    }

    static IEnumerable<PostDtw> GetPosts2()
    {
      DataTable postsDt = GetPostsDataTable();
      foreach (DataRow row in postsDt.Rows)
        yield return new PostDtw(row);
    }

    static DataTable GetPostsDataTable()
    {
      throw new NotImplementedException();
    }
  }

  /// <summary>
  ///This is the Base Class for all DataTable Wrappers
  /// </summary>
  public class BaseDataTableWrapper
  {
    public DataRow DataRow { get; set; }

    public BaseDataTableWrapper()
    {
    }

    public BaseDataTableWrapper(DataRow row)
      : this()
    {
      DataRow = row;
    }
  }

  #region [GetPost]

  /// <summary>
  ///This class is a wrapper around a DataTable,
  ///Associated with the stored procedure - GetPost
  ///This class provides a strongly typed interface to access data from the DataTable
  ///containing the result of the given stored procedure.
  /// </summary>
  public sealed class PostDtw : BaseDataTableWrapper
  {
    public Int32 PostId { get { return (Int32)DataRow[0]; } set { DataRow[0] = value; } }
    public DateTime PostDate { get { return (DateTime)DataRow[1]; } set { DataRow[1] = value; } }
    public String PostSlug { get { return (String)DataRow[2]; } set { DataRow[2] = value; } }
    public Int32 UserId { get { return (Int32)DataRow[3]; } set { DataRow[3] = value; } }
    public String PostTitle { get { return (String)DataRow[4]; } set { DataRow[4] = value; } }
    public String PostText { get { return (String)DataRow[5]; } set { DataRow[5] = value; } }
    public Boolean PostIsPublished { get { return (Boolean)DataRow[6]; } set { DataRow[6] = value; } }
    public Boolean PostIsPublic { get { return (Boolean)DataRow[7]; } set { DataRow[7] = value; } }
    public String PostTitleImg { get { if (DataRow[8] != DBNull.Value) return (String)DataRow[8]; else return default(String); } set { DataRow[8] = value; } }

    public PostDtw()
      : base()
    {
    }

    public PostDtw(DataRow row)
      : base(row)
    {
    }
  }

  #endregion [GetPost]

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