我应该将 SqlDataReader 代码放在哪个部分?
我对 C# 相当陌生,正在尝试编写一个 n 层 Web 应用程序。 为了确保我将逻辑和代码放在正确的位置,我只是有一个关于在哪里放置代码的问题。
我有三个主要部分:
DataAccess 代码 - 在我的 App_Code 文件夹内名为“BusinessLogic”的文件夹内。
业务逻辑代码 - 在我的 App_Code 文件夹内名为“DataAccess”的文件夹内。
表示层 - 所有 UI
举例来说,如果我需要编写一个 SqlDataReader 来从数据库中检索记录,我应该在哪里实际编写代码? 在 BLL 还是 DAL?
IE 从表示层我调用 BLL 代码。
ContentBLL content = new ContentBLL();
//some code to call the BLL layer...
这就是我开始感到困惑的地方。 在我调用的业务层逻辑层中,我是在此处编写 SqlDataReader 代码,还是再创建一个步骤并在数据访问级别中编写 SQlDataReader 代码。
IE 在 BLL 中我应该添加一个调用 DAL 的方法吗? EG
public static ContentBLL GetPageContent(intID)
{
return ContentDAL.GetItem(ID)
}
然后在我的 DAL 中我有一个方法来执行实际的 SqlDataReader EG
public static ContentBLL GetItem(int id)
{
//return the SqlDataReader code...
}
我一直在尝试从 asp.net 网站上的教程中学习,但是对于教程中的 DAL,他们使用数据集。 任何帮助将不胜感激。
I am fairly new to c# and am trying to write a n-tiered web application. To make sure that I put the logic and code in the right place I just have a question about where to put my code.
I have three main section:
DataAccess code - inside a folder named "BusinessLogic" inside my App_Code folder.
Business Logic code - inside a folder named "DataAccess" inside my App_Code folder.
The presentation layer - All the UI's
If for instance I need to write a SqlDataReader to retrieve records from my database where where would I physically write the code? In the BLL or the DAL?
IE From the presentation layer I call the BLL code.
ContentBLL content = new ContentBLL();
//some code to call the BLL layer...
This is where I start to get confused. In the Business layer Logic layer that I am calling do I write the SqlDataReader code here or do I create one more step and write the SQlDataReader code in the Data Access Level.
IE In the BLL should I add a method that called the DAL?
E.G
public static ContentBLL GetPageContent(intID)
{
return ContentDAL.GetItem(ID)
}
and then in my DAL I have a method to perform the actual SqlDataReader
E.G
public static ContentBLL GetItem(int id)
{
//return the SqlDataReader code...
}
I have been trying to learn from the tutorials on the asp.net website however for the DAL in the tutorial they use datasets instead.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的典型方法是我戏称的 2.5 层方法。
在此方法中,我使用以下方法:
业务层中的每个业务对象都有一个接受 IDataReader 的构造函数。 然后读取该读取器以填充该对象。
数据库层包装所有数据库访问请求并返回读者。
虽然这并不像某些人希望的那么纯粹,但另一种选择是制作愚蠢的容器类来编组层之间的数据,而且我更喜欢只使用 IDataReader。
此外,通过使用 IDataReader 而不是 SqlDataReader,我仍然与 DAL 松散耦合,并且可以实现任何形式的持久性,而不仅仅是 SQLServer。
My typical approach is what I jokingly call the 2.5 tier method.
In this method, I use the following approach:
Each Business Object in the Business Layer has a constructor that accepts a IDataReader. This reader is then read to populate the object.
The database tier is wraps up all DB access requests and returns readers.
While this not as pure as some people would like, the alternative is making dumb container classes to marshal the data between tiers, and I prefer to just use an IDataReader.
Additionally, by using a IDataReader and not a SqlDataReader, I'm still loosely coupled with my DAL and can implement any form of persistance, not just SQLServer.
在我看来,这听起来像是过度设计的典型案例。
我不会争论在学习阶段需要一点过度设计的事实,但我认为如果在任何时候它产生更多的混乱,你需要退后一步并重新考虑你的方法。
尝试多了解一点 ASP.NET,不要太在意语义。 如果您保持开放的态度并允许重构代码,您很可能能够自己给出问题的答案。
This sounds to me like a classic case of over-engineering.
I wont argue the fact that a little over-engineering is required during the learning phase but I think if at any point it generates more confusion you need to step back and reconsider your approach.
Try to learn a little bit more about ASP.NET and don't be so stressed out about the semantics. If you keep an open mind and allow for refactoring your code, you will likely be able to give the answer to your question yourself.