数据访问层(DAL)设计

发布于 2024-07-14 17:21:44 字数 336 浏览 4 评论 0原文

我正在使用 .Net 企业库数据访问应用程序块进行数据访问层设计。

在我的 Category DAL 类中,我有这样的方法:

GetProductsInCategory(int CatId)、GetAllProducts、GetCategories 等。

我的问题是:我应该把这行代码放在哪里?

DatabaseFactory.CreateDatabase("MyDB");

我应该将它放在上面的每个方法中,还是应该有一个基类将数据库对象返回到我的 DAL 类。

另外,我应该将这些 DAL 类方法保留为静态吗?

I'm using .Net enterprise library data access application block for my Data Access layer design.

In my Category DAL class, I've methods like :

GetProductsInCategory(int CatId), GetAllProducts, GetCategories, etc.

My question is: where do I put this line of code ?

DatabaseFactory.CreateDatabase("MyDB");

shall I put it in every method above or shall I have a base class which would return Database object to my DAL class.

Also, shall I keep these DAL class methods as static?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

·深蓝 2024-07-21 17:21:44

我更喜欢返回常见对象(例如连接)的基类,在您的示例数据库中。

以下是设计数据访问层的参考:.NET 应用程序架构:数据访问层

我使用 Microsoft Enterprise Library 数据访问应用程序块。 它完成了这里提到的大部分事情。 但像连接或交易这样的常见内容会转到我的基类。

替代文字

DataServiceBase类提供
常见的数据访问功能,例如
打开数据库连接,
管理交易、设置
存储过程参数,执行
命令等。 其他
换句话说,DataServiceBase 类
包含通用数据库代码和
为您提供一套帮手
个人数据的使用方法
服务类。 得出的数据
服务类使用辅助方法
在DataServiceBase中具体
目的,例如执行特定的
命令或运行特定查询。

I prefer a base class that returns common objects like connection, in you example Database.

Here is reference to desing Data access Layer : .NET Application Architecture: the Data Access Layer

I use Microsoft Enterprise Library Data Access Application Block. It does most of the things mentioned here. but common stuff like connections or transactions goes to my base class.

alt text

The DataServiceBase class provides
common data access functionality like
opening a database connection,
managing a transaction, setting up
stored procedure parameters, executing
commands, and so forth. In other
words, the DataServiceBase class
contains the general database code and
provides you with a set of helper
methods for use in the individual data
service classes. The derived data
service classes use the helper methods
in the DataServiceBase for specific
purposes, like executing a specific
command or running a specific query.

看海 2024-07-21 17:21:44

感谢您的提示..我将拥有从基类 DBManager 派生的所有 DAL 类。该类将有一个名为 GetDatabase() 的受保护方法,该方法将包含代码: return DatabaseFactory.CreateDatabase("MyDB"); 我在派生类中的方法如下所示:..

public DataSet GetProductsInCategory(int Category) 
{
Database db = base.GetDatabase(); 
DbCommand dbCommand = db.GetStoredProcComman("GetProductsByCategory"); 
db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, Category); 
return db.ExecuteDataSet(dbCommand);
} 

这个 DAL 设计看起来还好吗?

thanks for ur tips..i will have all my DAL classes derived from a base class DBManager.This class will have a protected method called GetDatabase() which will have code: return DatabaseFactory.CreateDatabase("MyDB"); And my method in derived class would look like:..

public DataSet GetProductsInCategory(int Category) 
{
Database db = base.GetDatabase(); 
DbCommand dbCommand = db.GetStoredProcComman("GetProductsByCategory"); 
db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, Category); 
return db.ExecuteDataSet(dbCommand);
} 

does this DAL design look ok?

后eg是否自 2024-07-21 17:21:44

我建议考虑使用 SubSonic 进行 DAL 和对象生成。 它实现了 Microsoft 的应用程序块,但为您提供了简单(且强大)的查询功能,例如;

SubSonic.Query qry = new SubSonic.Query(Usr.Schema);
qry.SetSelectList(Usr.Columns.UserPkid);
qry.QueryType = SubSonic.QueryType.Select;
qry.AddWhere(Usr.UsernameColumn.ColumnName, SubSonic.Comparison.Equals, Username);

using (IDataReader reader = qry.ExecuteReader())
{
    while (reader.Read())
    {
        Trace.WriteLine("Fetched User Pkid [" + reader[Usr.Columns.UserPkid].ToString() + "]");
    }
}

当然,它实现了 ActiveRecord 模式,因此对象类具有 .Delete()、. Add()、.Get() 类型方法。

I would suggest looking at using SubSonic for DAL and object generation. It implements Microsoft's Application Block but gives you simple (and robust) query capabilites like;

SubSonic.Query qry = new SubSonic.Query(Usr.Schema);
qry.SetSelectList(Usr.Columns.UserPkid);
qry.QueryType = SubSonic.QueryType.Select;
qry.AddWhere(Usr.UsernameColumn.ColumnName, SubSonic.Comparison.Equals, Username);

using (IDataReader reader = qry.ExecuteReader())
{
    while (reader.Read())
    {
        Trace.WriteLine("Fetched User Pkid [" + reader[Usr.Columns.UserPkid].ToString() + "]");
    }
}

And of course it implements the ActiveRecord pattern so the object classes have the .Delete(), .Add(), .Get() type methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文