3

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?

4

3 回答 3

3

我更喜欢返回像连接这样的常见对象的基类,在你的例子中是数据库。

这是对设计数据访问层的参考:.NET 应用程序体系结构:数据访问层

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

替代文字

DataServiceBase 类提供常见的数据访问功能,例如打开数据库连接、管理事务、设置存储过程参数、执行命令等。换句话说,DataServiceBase 类包含通用数据库代码,并为您提供了一组帮助方法,供您在各个数据服务类中使用。派生的数据服务类将 DataServiceBase 中的帮助器方法用于特定目的,例如执行特定命令或运行特定查询。

于 2009-03-04T21:54:54.680 回答
1

感谢您的提示..我将拥有从基类 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 设计看起来不错吗?

于 2009-03-05T02:39:48.207 回答
0

我建议考虑使用 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() 类型的方法。

于 2009-03-05T04:30:33.137 回答