DAL vs BLL – Choosing the Right Return Types

business-logiccode-organizationlayers

Here is how I see the main differences between the DAL and the BLL:

  • The DAL directly query the database.
  • The DAL doesn't create new information. It may execute complex sql requests, joining several tables, but the result of such a request never gives any information that wasn't already in the database.
  • The BLL applies a business logic on the DAL's informations to create new informations.

Now, I wonder what datatypes these layers' functions are supposed to return. For the moment, all my DAL's function return DataTable. But my BLL's functions return some class or list of class that I created.

For example:

  • dal.getAllPortfolios() returns a DataTable (I'm working with Visual Studio), result of a join on several tables.
  • bll.getAllPortfolios() calls dal.getAllPortfolios, but return a List of Portfolio. With my public Portfolio class being in the BLL's namespace.

Sometimes, the BLL function doesn't really apply any "business logic" but only map the DataTable from de DAL into a BLL's class.

I wonder if this is ok. Maybe all the mapping work should be done by the DAL?
If this is the case, where to put my personal mapping classes? In the BLL or DAL namespace (those classes being required in layers above the BLL)?

Best Answer

I would say that's perfectly acceptable - even desirable. The DAL should only be Data Access - that's why it's the Data Access Layer. The BL, on the other hand, should never interact directly with the database, but should take the data retrieved from the DAL and put it in a more consumer- or developer-friendly format to be leveraged in your code.

By maintaining this separation of concerns, if you ever changed databases (either database structure, or database type, or the physical server itself) you only need to make changes in the DAL. Generally speaking, the DAL will only have basic methods for executing stored procedures, queries, etc., and returning single values or data tables/data sets. It sends those back to the BLL, which then creates the necessary objects to be consumed. If you want an easy (if overly-simplified) way to visualize it, a Google Search popped up this image: enter image description here