过度的DataTable用法不好? [英] Is excessive DataTable usage bad?

查看:183
本文介绍了过度的DataTable用法不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近被要求协助另一支球队建设ASP .NET网站。他们已经有书面code的显著量 - 我特意要求建立该网站的几个单独的页面

I was recently asked to assist another team in building an ASP .NET website. They already have a significant amount of code written -- I was specifically asked build a few individual pages for the site.

在探索code的网站的其余部分,正在建设中的数据表的数量我跳了出来。作为一个在该领域相对较新的,我从来没有工作过上使用数据库不亚于此网站的应用程序,所以我不知道有多普遍,这是。似乎只要将数据从我们的数据库查询,结果被存储在一个DataTable。该数据表,然后通常是由自身传来传去,或者它传递给构造函数。这与一个DataTable的初始化类总是的DataTable中分配给私有/受保护的领域,但只有少数这些类实现IDisposable。事实上,在千code行的,我到目前为止浏览,我还没有看到叫上一个DataTable Dispose方法。

While exploring the code for the rest of the site, the amount of DataTables being constructed jumped out at me. Being a relatively new in the field, I've never worked on an application that utilizes a database as much as this site does, so I'm not sure how common this is. It seems that whenever data is queried from our database, the results are stored in a DataTable. This DataTable is then usually passed around by itself, or it's passed to a constructor. Classes that are initialized with a DataTable always assign the DataTable to a private/protected field, however only a few of these classes implement IDisposable. In fact, in the thousands of lines of code that I've browsed so far, I have yet to see the Dispose method called on a DataTable.

如果任何东西,这似乎并不好OOP。这一点是我应该担心?或者,我只是更注重细节比我应注意什么?假设你是最有经验的开发人员比我,你会怎么感觉或反应,如果谁只是指派专人协助您与您的网站走近你这个问题?

If anything, this doesn't seem to be good OOP. Is this something that I should worry about? Or am I just paying more attention to detail than I should? Assuming you're most experienced developers than I am, how would you feel or react if someone who was just assigned to help you with your site approached you about this "problem"?

推荐答案

数据表可以用于善恶。

可接受的使用

我会发现下面是一个数据表或一个DataRow的可接受的使用:

I would find the following to be an acceptable use of a datatable or a datarow:

public class User
{
    private DataRow Row { get; set; };
    public User(DataRow row) { this.Row = row; }

    public string UserName { get { return (string)Row["Username"]; } }
    public int UserID { get { return (int)Row["UserID"]; } }
    public bool IsAdmin { get { return (bool)Row["IsAdmin"]; } }
    // ...
}

上面的类的确定的,因为其映射DataRow中类型安全的类。相反,用绳子和类型化的数据行的工作,现在你有真正的数据类型和智能为您提供帮助。此外,如果你的数据库模式的变化,你可以在你的对象修改,而不是修改列名的任何地方使用的列名。最后,你可以像dtaccount_created丑列名在名为AccountCreated属性​​映射。

The class above is ok because it maps a DataRow to a typesafe class. Instead of working with strings and untyped datarows, now you have real datatypes and intellisense to assist you. Additionally, if your database schema changes, you can modify a column name in your object, instead of modifying the column name everywhere its used. Finally, you can map ugly column names like "dtaccount_created" to a property named "AccountCreated".

当然,真的没有很好的理由来写这个包装类,因为Visual Studio将自动生成类型数据集的为您服务。或者,作为替代,像NHibernate的一个很好的ORM允许您定义类似于上面一类。

Of course, there's really no good reason to write this wrapper class, since Visual Studio will automatically generate typed datasets for you. Or, as an alternative, a good ORM like NHibernate allows you to define classes similar to the one above.

不管你,你应该使用普通的旧ADO.NET,类型化数据集,还是一个完全成熟的ORM取决于应用程序的需求和复杂性。很难说你的团队是否在做正确的事withotu居然看到了一些样品code。

Whether you you should use plain old ADO.NET, typed datasets, or a full fledged ORM depends on the requirements and complexity of your application. Its hard to say whether your team is doing the right thing withotu actually seeing some sample code.

此外,我偶尔发现它有用数据绑定列表和网格有一个DataTable,因为改变底层数据行会自动导致GUI刷新。如果您创建自己的类型安全的包装,那么你需要手动执行IPropertyChanging和IPropertyChanged接口。

Additionally, I occasionally find it useful to databind lists and grids with a datatable, because changes to the underlying datarow will automatically cause the GUI to refresh. If you create your own type-safe wrapper, then you need to implement the IPropertyChanging and IPropertyChanged interfaces manually.

使用不合格

不幸的是,我见过的程序员使用的临时容器数据表,替代类等,如果你看到你的团队做这个,在他们扔石头。编程的这种风格只是没有在静态类型语言工作,它要使发展成为一场噩梦。

Unfortunately, I've seen programmers use datatables for ad hoc containers, alternatives to classes, etc. If you see your team doing this, throw rocks at them. That style of programming just doesn't work in a statically typed language, and its going to make development a nightmare.

与数据表主要问题:他们不是类型化,所以你不能没有给他们一个字符串,铸造任何神秘物体它们包含到正确的类型做任何与他们有用。此外,重构一个列名几乎是不可能的,因为自动它们是基于字符串的,所以你不能依靠智能感知,帮助您编写正确code,你不能赶在编译时错误。

Main problem with datatables: they aren't typed, so you can't do anything useful with them without giving them a string and casting whatever mystery object they contain into the right type. Additionally, refactoring a column name is nearly impossible to automate since they are based on strings, so you can't rely on intellisense to help you write correct code, and you can't catch errors at compile time.

我说相信自己的直觉:如果你想设计古怪,它可能是

I say trust your instinct: if you think design is flakey, it probably is.

这篇关于过度的DataTable用法不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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