Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Access Layer (DAL) design

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?

like image 806
Jeremy Thomson Avatar asked Feb 01 '26 00:02

Jeremy Thomson


2 Answers

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.

like image 143
Canavar Avatar answered Feb 03 '26 13:02

Canavar


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?

like image 20
Jeremy Thomson Avatar answered Feb 03 '26 13:02

Jeremy Thomson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!