数据表包装或如何从业务逻辑中分离出UI [英] DataTable Wrapper or How to decouple UI from Business logic

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

问题描述

我使用的web表单,C#,Asp.net。
我们都知道,在这个模型的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?

我想用这个例子是:
我有一个GridView和DataTable(GridView控件绑定到DataTable和DataTable从存储过程喂养)。

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).

我想在GridView(UI)和DataTable(业务逻辑)来进行去耦。

I would like the GridView (UI) and DataTable (business logic) to be decoupled.

是否值得写一个DataTable的包装?是已被证明和测试,你可以推荐应遵循?有切实可行的模式。

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?

如果有经验的人可能会提供一些​​线索,这将是真棒。
而且,作为一个最后要注意,我想说,ASP MVC是不是现在的选项,因此不建议这样做。

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.

我的数据库访问层返回DataTable。
注意我,因为这是公司的政策,以使用此数据库层。

My database access layer returns a DataTable. Note that I HAVE to use this database layer as this is a company policy.

推荐答案

我通过这一去,而最近从我们的UI层去耦同样的事情。

I went through this recently while decoupling much the same thing from our UI layer.

您可以看到我的进步<一个href=\"http://stackoverflow.com/questions/583689/dictionaryt-of-listt-and-listviews-in-asp-net\">here和这里

You can see my progress here and here.

在我看来,A 数据表做的不可以再present业务逻辑。具体地,它的数据从数据库中直接拉出。业务逻辑变成数据转换成一个真正有用的业务对象。

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.

您可以做到这一点通过创建对象和列表&LT;对象&gt; 组成数据表和数据表的集合,然后你就可以说显示这些对象的ListView。我弥补我上面贴的链接后面的步骤。和前步骤是一样容易以下内容:

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. 创建一个类,将重新present你的对象。

  2. 通过您的DataTable迭代(或数据集,或者不管你检索数据),并推这些领域进入(或列表&LT; T&GT; )的对象的属性;

  3. 的列表返回到GridView或ListView显示。

  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.

这样,你的ListView或GridView的将不紧耦合到您检索的数据的方法。如果你决定从JSON查询或稍后一个XML文件,让你的数据会发生什么?然后,你就必须建设成为有这一点。

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.

有多种方法来从数据库中获取数据,没有办法,我可以通过所有的人都在这里。我假设你已经知道如何从数据库中检索数据,如果你不这样做,有<一个href=\"http://www.google.com/search?hl=en&q=Get%2BData%2Bfrom%2BDatabase%2BC%23&btnG=Google%2BSearch&aq=f&oq=\"相对=nofollow>不少环节来遵循。让我们pretend您连接到数据库,并使用 SqlDataReader的检索数据。我们将有回升。

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.

Foo
----
id
Name
Description

和这里的方法:

 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));
            }
        }

一旦出现这种情况,你可以通过在,而循环为目标的 SQLDataReader.Read()功能。

一旦你这样做,让你的返回pretend是一个列表。如果你这样做,然后按照我上面给了一个链接,可以取代词典&LT; TKEY的,TValue&GT; 列表&LT; T&GT; 和获得相同的结果(有微小的差异)。在属性类只包含在数据库中的列名,所以你有一个地方改变它们(如果​​你想知道)。

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).

这篇关于数据表包装或如何从业务逻辑中分离出UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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