使用数据表为什么在UI是错误的? [英] Why Using DataTable In UI Is Wrong?

查看:179
本文介绍了使用数据表为什么在UI是错误的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者。我发现这很难理解这里的一个概念。

I am a beginner. I am finding it very difficult to understand one concept here.

有人告诉我,我不应该在UI层面使用数据表。

I have been told that I should not use DataTable in UI level.

请帮我理解这个概念。

解决方案:

MyApp.Data
MyApp.Logic
MyApp.Web

MyApp.Web引用MyApp.Logic和MyApp.Logic引用MyApp.Data。

MyApp.Web references MyApp.Logic, and MyApp.Logic references MyApp.Data.

在我的申请,我所要做的仅仅是由表名,这是从一个DropDownList选择绑定一个gridview。但是,也有数百多个表。这里的目的仅仅是要显示数据显示给用户(寻呼)。

On my application, what I am trying to do is simply bind a gridview by the tablename, which is chosen from a dropdownlist. However, there are hundreds and more tables. The purpose here is just to display data to the user (with paging).

因此​​,对项目MyApp.Data类GET_DATA有一个功能:

So a class "Get_Data" on project MyApp.Data has a function:

public static DataTable Get_DataTable_By_Name(string Table_Name)
{
    //Check and santize the table name for possible SQL injection attack.
    //SELECT FROM DATABASE TABLE.
    return DataTable;
}

从MyApp.Logic,我只是路过的DataTable从MyApp.Data层来MyApp.Web。然后GridView的必然,一切都在这里工作。

From MyApp.Logic, I am just passing the datatable from the MyApp.Data tier to MyApp.Web. And then the gridview is bound and everything is working here.

所以我在做什么错在这里?是不是真的不好在UI层面的数据表?为什么是一个不好的设计?

So what I am doing wrong here? Is it really bad to have datatable in UI level? Why is it a bad design?

推荐答案

的概念,这是不好的做法,为你的UI创建/负载/消费的DataTable来自想法,它是preferable分裂您的应用程序成专门的层,最普通的裂是有3层:

The notion that it is bad practice for your UI to create/load/consume a datatable comes from the idea that it is preferable to split your application into layers that specialize, the most common split is to have 3 layers:

1层负责与数据库通信和填充域对象的数据层,领域对象在第2层也持有业务逻辑应用程序通常定义。

Layer 1 is a data layer responsible for communicating with the database and populating domain objects, the domain objects are typically defined in layer 2 which also holds the business logic for your application.

这是我的意思是重新present实实在在的东西,如客户,银行账号,酒店客房等类别域对象
并通过业务逻辑我的意思的事件,即当一个酒店房间预订的确认发送到客户中应用到的域对象的规则。

by domain objects i mean classes that represent real things like customer, bank account, hotel room etc. and by business logic i mean the rules that apply to domain objects during events i.e. when a hotel room is booked a confirmation is sent to the customer.

第三层是UI层,以simplyfy编码这应该只处理什么在2层,这是哪里的建议,不是在你的UI使用的DataTable的由来。

the third layer is the UI layer, to simplyfy coding this should only deal with whats in layer 2, this is where the recommendation to not use datatables in your UI comes from.

动机分裂您的应用程序真正开始有道理,当你的应用是很大的,或者你有多个开发人员的工作就可以了,或者你使用单元测试。如果在这种情况下是那么我想尝试并找到自己的团队中的一员友好解释这更好的,但如果这只是你在写你自己的应用程序,然后我就不会担心太多,写很多code的,读很多书,这东西会更有意义,我会建议UML和模式的克雷格Larman与本书。

the motivation to split your application really start to make sense when your application is big, or you have multiple developers working on it, or your using unit testing. If you're in that situation then i'd try and find a friendly member of your team to explain this better, but if this is just you writing an application on your own then i wouldn't worry too much about it, write lots of code, read lots of books and this stuff will make more sense, i would recommend the craig larman book on uml and patterns.

希望这有助于

这篇关于使用数据表为什么在UI是错误的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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