从 DAL 返回 DataTable 或 DataSet 是错误的方法 [英] Is returning DataTable or DataSet from DAL is wrong approach

查看:20
本文介绍了从 DAL 返回 DataTable 或 DataSet 是错误的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从头开始创建我的分配项目,我首先创建了一个处理 SQL 连接并可以执行存储过程的 DAL.
以后的项目会是CMS或者Blog
我的 DAC 或 DAL 正在从 web.config 读取连接字符串并接受 SQL 参数数组 以及 Stored Procedure Name 并返回在DataTable中执行存储过程.
我按如下方式调用我的 DAC(使用类函数,有时使用后面的代码)

I am creating my assignment project from scratch, I started off by creating a DAL that handles connection to SQL and can execute stored procedure.
The project later will be a CMS or Blog
My DAC or DAL is reading connection string from web.config and accepting arrays of SQL Parameters along with Stored Procedure Name and is returning output of the executed stored procedure in DataTable.
I am calling my DAC as follows(using class functions and sometimes using code behind)

Dim dt As DataTable = Dac.ReturnDataTable("category_select_bypage",  New SqlParameter("@pageid", pageid), New SqlParameter("@id", id))

哪个工作正常,我假设通过调用这个 DAC 并使用存储过程插入和检索数据来完成我的整个应用程序,或者以后可能是简单的查询.
但是,我已经向我的老师展示了这项工作,他告诉我 您使用了错误的方法,并且您的代码没有实现正确的 DAL 方法.
所以,我现在对 DAL 和 DAC 太困惑了.Any 这里是主要问题.

1. DAL和DAC的真正区别是什么
2. 用我的方法,用它做什么样的应用比较好?我可以使用这种方法制作购物车吗?3. 我的方法有什么问题?

Which is working fine and i am assuming to complete my whole application by calling this DAC and insert and retrieve data using stored procedures or may be simple queries later.
But, i have showed this work to my teacher and he told's me that you are using a wrong approach and your code is not fulfilling the correct DAL approach.
So, i am now too much confused with DAL and DAC. Any How here are main questions.

1. What really is the difference between DAL and DAC
2. By using my approach, what kind of application is good to make with it ? Can i make shopping carts using this approach? 3. What is wrong with my approach ?

推荐答案

Adriano 提供了一个很好的链接.当然是必读的.但我确实看到了一件事.在应用程序中创建逻辑层时,每一层都不知道其下层的内部工作原理.例如表示层不知道数据层如何获取数据,它只是魔术.原因是如果您决定不使用 SqlClient 并选择不同的技术会怎样.现在通过对图层使用技术隐藏",您可以很容易地进行更改.

Adriano provided a very good link. Certainly a must read. But I do see one thing. When creating logical layers in an application, each layer is not aware of the inner workings of those beneath it. For example the presentation layer has no idea how the data layer gets data, it just magic. The reason for this is what if you decide to not use the SqlClient and chose a different technology. Now by using "Technology Hiding" with layers you can make that change quite easily.

因此,鉴于您所拥有的,我假设 DataTable 位于表示(或应用程序)层中.而且,如果这是真的,您的 DAC 方法调用不应公开任何与检索数据的内容或方式相关的内容.在您的情况下,SqlParameter 参数违反了此规则.也许你可以传递字符串或整数.例如:

So given what you have I'm assuming the DataTable is in the presentation (or application) layer. And, if this is true your DAC method call should not expose anything related to what or how it is retrieving data. In your case this rule is violated by the SqlParameter parameter. Perhaps you could pass string's or int's. For example:

public DataTable Dac.GetPage(int pageId, int id)

无论如何,祝你好运.我很高兴看到有人愿意学习,有人愿意教.

Never the less, best of luck. I'm glad to see those willing to learn and those willing to teach.

这篇关于从 DAL 返回 DataTable 或 DataSet 是错误的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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