DataTable Wrapper 或如何将 UI 与业务逻辑解耦
我正在使用 Web 表单、C#、Asp.net。 众所周知,在这个模型中,UI和业务逻辑经常是混合在一起的,如何有效地将它们分开呢?
我想使用的例子是: 我有一个 GridView 和一个 DataTable(GridView 绑定到 DataTable 并且 DataTable 由存储过程提供)。
我希望 GridView(UI)和 DataTable(业务逻辑)能够解耦。
为 DataTable 编写包装器值得吗? 是否有经过证明和测试的实用模式可供您推荐遵循?
如果有经验的人可以提供一些线索,那就太棒了。 最后,我想说 ASP MVC 目前不是一个选择,所以不推荐它。
我的数据库访问层返回一个数据表。 请注意,我必须使用此数据库层,因为这是公司政策。
I am using web forms, C#, Asp.net.
As we all know, in this model UI and business logic are often mixed in. How do I separate these effectively?
The example I would like to use is:
I have a GridView and a DataTable (GridView binds to the DataTable and DataTable is fed from the stored procedure).
I would like the GridView (UI) and DataTable (business logic) to be decoupled.
Is it worth it to write an wrapper for DataTable? Are there practical patterns that have been proved and tested that you could recommend to be followed?
If someone with experience could shed some light, that would be awesome.
And, as a final note I would like to say that ASP MVC is not an option right now, so don't recommend it.
My database access layer returns a DataTable.
Note that I HAVE to use this database layer as this is a company policy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近经历了这个,同时将很多相同的事情与我们的 UI 层解耦。
您可以在此处和此处。
在我看来,
DataTable
确实不代表业务逻辑。 具体来说,它是直接从数据库中提取的数据。 业务逻辑将数据转变为真正有用的业务对象。那么,第一步是将 DataTable 与 Business 对象解耦。
您可以通过创建组成 DataTable 和 DataTable 集合的对象和
List
List
)的属性中;这样,您的 ListView 或 Gridview 就不会与您检索数据的方法紧密耦合。 如果您稍后决定从 JSON 查询或 XML 文件获取数据,会发生什么情况? 然后你必须将其构建到那里。
步骤 1 - 从数据库获取数据
有多种方法可以从数据库获取数据,我无法在此一一介绍所有方法。 我假设您已经知道如何从数据库中检索数据,如果您不知道,可以使用 相当多的链接可供关注。 假设您已连接到数据库,并使用
SQLDataReader
检索数据。 我们会在那里接载。类图
方法如下:
一旦发生这种情况,您可以通过在针对
SQLDataReader.Read()
函数的while
循环中执行该过程来返回一个列表。完成此操作后,我们假设返回的
Foo
是一个列表。 如果您这样做,并按照我上面给出的第一个链接进行操作,您可以将Dictionary
替换为List
并获得相同的结果(使用较小的差异)。Properties
类仅包含数据库中的列名称,因此您可以在一个地方更改它们(如果您想知道的话)。数据表 - 基于注释更新
您始终可以插入中间对象。 在本例中,我将在 DataTable 和 UI 之间插入一个业务层,并且我已经在上面讨论了我要做什么。 但 DataTable 不是业务对象;而是业务对象。 它是数据库的可视化表示。 您不能将其传输到 UI 层并将其称为解耦。 他们说您必须使用 DataTable,他们是否说您必须将该 DataTable 传输到 UI? 我无法想象他们会这么做。 如果你这样做,那么你将永远不会脱钩。 您始终需要在 DataTable 和 UI 层之间有一个中间对象。
I went through this recently while decoupling much the same thing from our UI layer.
You can see my progress here and here.
In my opinion, A
DataTable
does not represent business logic. Specifically, it's data pulled directly from the database. Business logic turns that data into a truly useful business object.The first step, then, is to decouple the DataTable from the Business object.
You can do that by creating objects and
List<object>
that make up DataTables and Collections of DataTables, and then you can make a ListView that displays those Objects. I cover the latter steps in the links I posted above. And the former steps are as easy as the following:List<T>
);This way your ListView or Gridview won't be tightly coupled to the method that you are retrieving your data. What happens if you decide to get your data from a JSON query or a XML file later on? Then you'd have to build this into there.
Step 1 - Getting Data From Database
There are multiple methods to get data from a database, there's no way I can go through all of them here. I assume that you already know how to retrieve data from a database, and if you don't, there are quite a few links to follow. Let's pretend you've connected to the database, and are using an
SQLDataReader
to retrieve data. We'll pick up there.Class Diagram
And here's the method:
Once that happens, you can return a list by going through that process in a
while
loop that targets theSQLDataReader.Read()
function.Once you do that, let's pretend that your
Foo
being returned is a List. If you do that, and follow the first link I gave above, you can replaceDictionary<TKey, TValue>
withList<T>
and achieve the same result (with minor differences). TheProperties
class just contains the column names in the database, so you have one place to change them (in case you were wondering).DataTable - Update Based on Comment
You can always insert an intermediate object. In this instance, I'd insert a Business Layer between the DataTable and the UI, and I've discussed what I'd do above. But a DataTable is not a business object; it is a visual representation of a database. You can't transport that to the UI layer and call it de-coupled. They say you have to use a DataTable, do they say that you have to transport that DataTable to the UI? I can't imagine they would. If you do, then you'll never be de-coupled. You'll always need an intermediate object in between the DataTable and the UI layer.
我首先将数据表解耦到垃圾桶中。 构建一个领域层,然后构建某种类型的处理数据库的数据访问层(推荐 ORM)。
然后构建一个向 UI 提供数据的服务层。 所有业务逻辑都应该位于服务或实体本身内。
I'd start by decoupling the data table right into the trash can. Build a domain layer, and then some type of data access layer which deals with the DB (ORM recommended).
Then build a servicing layer which provides the data to the UI. All business logic should be within the service or the entities themself.
考虑实施 MVP(模型视图演示者)模式。 它使您可以通过演示者界面分离业务逻辑,这也允许更好的单元测试功能。 然后,aspx 页面的代码隐藏只是事件的连接器和属性的 getter/setter。 您可以在 MS 模式和实践企业应用程序块(CAB - 复合应用程序块 - 如果我没有记错的话)中找到它。
您可以在这里阅读更多相关信息: http://msdn.microsoft.com/ en-us/magazine/cc188690.aspx
但从 DataTable/DataSet 到对象 (POCO) 也是首选。
Consider implementing MVP (model view presenter) pattern. It gives you separation of biz logic through presenter interface, which also allow better unit testing capabilities. Your codebehind of aspx page is then just connector of events and getter/setter of properties. You can find it in MS pattern&practices enterprise application blocks (CAB - composite application block - if i'm not mistaking).
You can read more about it here: http://msdn.microsoft.com/en-us/magazine/cc188690.aspx
But also going from DataTable/DataSets to objects (POCO) is preferred.