DataTable Wrapper或如何将UI与业务逻辑分离 [英] DataTable Wrapper or How to decouple UI from Business logic

查看:184
本文介绍了DataTable Wrapper或如何将UI与业务逻辑分离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Web表单,C#,Asp.net。
众所周知,在这个模型中,UI和业务逻辑往往混合在一起。我如何有效地分离这些?



我想使用的示例是:
我有一个GridView和一个DataTable(GridView绑定到DataTable和DataTable从存储过程中提供)。



我想要GridView UI)和DataTable(业务逻辑)进行解耦。



是否值得为DataTable编写一个包装器?是否有实际的模式已被证明和测试,你可以建议遵循?



如果有经验的人可以摆脱一些光,那将是非常棒的。
而且,作为最后一个笔记,我想说ASP MVC现在不是一个选项,所以不要推荐它。



我的数据库访问图层返回一个DataTable。
请注意,我必须使用这个数据库层,因为这是一个公司政策。

解决方案

同时从我们的UI层解脱大量相同的东西。



您可以看到我的进度这里的。



在我看来,A DataTable 表示业务逻辑。具体来说,它是直接从数据库中拉出的数据。业务逻辑将数据转化为真正有用的业务对象。



然后,第一步是将DataTable与Business对象分离。



您可以通过创建构成DataTable和DataTable集合的对象和 List< object> 那么您可以使一个ListView显示这些对象。我覆盖了上面发布的链接中的后面的步骤。前面的步骤和以下一样简单:


  1. 创建一个代表你的对象的类。

  2. 在数据表(或DataSet中,或者是您检索数据)中迭代,并将这些字段推送到该对象的属性(或 List< T> )中;

  3. 将该列表返回到要显示的Gridview或ListView。

这样,您的ListView或Gridview将不会与您检索数据的方法紧密耦合。如果您决定从JSON查询或XML文件中获取数据,会发生什么?那么你必须将它建立到那里。



步骤1 - 从数据库获取数据



从数据库获取数据有多种方法,没有我可以通过所有这些方式在这里。我假设您已经知道如何从数据库中检索数据,如果没有,则相当多的链接。假设你连接到数据库,并使用 SQLDataReader 来检索数据。我们会在那里拿起



类图



  Foo 
----
id
名称
描述

这里的方法是: / p>

  private void FillDefault(SqlDataReader reader,foos foo)
{
try
{
foo.id = Convert.ToInt32(reader [Foo.Properties.ID]);
foo.Name = reader [Foo.Properties.NAME] .ToString();


if(!string.IsNullOrEmpty(
reader [Foo.Properties.DESCRIPTION] .ToString()))
foo.Description =
reader [Foo.Properties.DESCRIPTION] .ToString();
else foo.Description = string.Empty;
}
catch(Exception ex)
{
throw new Exception(
string.Format(Invalid Query。
Column'{0}'不存在于SqlDataReader中。,
ex.Message));
}
}

一旦发生这种情况,您可以返回列表通过循环中的进程,该循环定位到 SQLDataReader.Read()函数。



一旦你这样做,我们来假设你所返回的 Foo 是一个列表。如果你这样做,并按照我上面提供的第一个链接,你可以用 List< T> Dictionary< TKey,TValue> c $ c>并获得相同的结果(略有不同)。 属性类只包含数据库中的列名,因此您有一个地方可以更改它们(以防您想知道)。



DataTable - 基于评论的更新



您可以随时插入一个中间对象。在这种情况下,我会在DataTable和UI之间插入一个业务层,我已经讨论过我在上面做了什么。但是DataTable不是业务对象;它是数据库的可视化表示。您不能将其传输到UI层,并将其称为脱耦合。他们说您必须使用DataTable,他们是否表示您已将将该DataTable传输到UI?我不能想象他们会的。如果你这样做,那么你永远不会被解耦。您将始终需要DataTable和UI层之间的中间对象。


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.

解决方案

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:

  1. Create a class that will represent your object.
  2. iterate through your DataTable (or DataSet, or however you retrieve the data) and shove those fields into properties of that object (or that List<T>);
  3. return that List to the Gridview or ListView to display.

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

Foo
----
id
Name
Description

And here's the method:

 private void FillDefault(SqlDataReader reader, Foos foo)
        {
            try
            {
                foo.id = Convert.ToInt32(reader[Foo.Properties.ID]);
                foo.Name = reader[Foo.Properties.NAME].ToString();


             if (!string.IsNullOrEmpty(
                reader[Foo.Properties.DESCRIPTION].ToString()))
                 foo.Description = 
                 reader[Foo.Properties.DESCRIPTION].ToString();
             else foo.Description = string.Empty;
            }
            catch (Exception ex)
            {
               throw new Exception(
               string.Format("Invalid Query. 
               Column '{0}' does not exist in SqlDataReader.", 
               ex.Message));
            }
        }

Once that happens, you can return a list by going through that process in a while loop that targets the SQLDataReader.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 replace Dictionary<TKey, TValue> with List<T> and achieve the same result (with minor differences). The Properties 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.

这篇关于DataTable Wrapper或如何将UI与业务逻辑分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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