Reputation: 999
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?
Upvotes: 3
Views: 3332
Reputation: 999
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?
Upvotes: 1
Reputation: 2282
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.
Upvotes: 0
Reputation: 48108
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.
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.
Upvotes: 3