What should we have in a Business Logic Layer (BLL)
Asked Answered
H

3

6

I know this is a very basic question. But I am really not able to comprehend what should we have in BLL. Let me take an example.
Let us consider a Login.aspx web page, which is used to facilitate user login.
In this case Login.aspx will have two textboxes and one login button.(Presentation Layer).
Data Access Layer will have functions to check if username and password are correct.


I don't think I need something else in this page. So what will I have in BLL. If you want to add some functionality that should come in BLL, please add.

Hausmann answered 22/1, 2010 at 16:38 Comment(0)
C
3

You should have something like this:

The UI calls BL.SaveUsernameAndPassword(string user, string pass);

BL.SaveUsernameAndPassword should validate the strings, and then call DAL.SaveUsernameAndPassword(string user, string pass);

DAL.SaveUsernameAndPassword should put these parameters into your SQL query and execute it, with the assumption that the data is valid

Consensual answered 22/1, 2010 at 16:50 Comment(5)
so in this case I can't have a common DAL for all my projects, as I have to write query in DAL.Hausmann
Why does having to write the query in the DAL prevent you from having a common DAL for your projects?Consensual
In some projects I may want to select different number of variables from different tables.Hausmann
Hmm, in that case then you should have a different DAL per project. The BL would then choose which DAL to use, depending on the project.Consensual
Your DAL should literally be a direct map onto your database. You don't want it to contain any logic, or do anything clever like map onto different databases, it just conveys data back and forth.Consensual
B
7

No, the BLL checks if the username and password are correct. The DAL is only for data access.

Blamed answered 22/1, 2010 at 16:40 Comment(4)
so you mean, on click of login button, i will call a function in BLL which checks if username and password is correct by passing them to DALHausmann
@kprobst, should I write a Sql query to in BALHausmann
For example, let's say that you're retrieving a password hash based on the user name, and verifying it against the provided password. The DAL is used to pull the data off the table where that is stored, the BLL is used to verify the hash. In general no logic should reside in the database, whether business or not. That goes in the business layer. This is not always possible, but you should strive to keep as much code out of the DB as possible. Consider the DAL (ORM, whatever) part of the DB for this purpose.Blamed
No, the DAL is used only to pull the data from the database. That's the definition of DAL, unless you're confusing it with ORM.Blamed
P
5

"Data Acess Layer will have functions to check if username and password are correct" - wrong. The BLL will do that, the DAL will only retrieve (or try to retrieve) the user's information, without doing any checking on it.

Pothook answered 22/1, 2010 at 16:41 Comment(5)
so in this case, I am executing a SP, then where should I add Parameters to that SP, in BLL or DALHausmann
Your BLL should ask the DAL to call the SP, and the BLL will interpret its results upon its return. The DAL is only a mostly brainless conduit.Adhern
so it means in BLL I will add parameters to that SP and pass it to DAL for execution.Hausmann
No, the BLL knows nothing about SP's and parameters. The DAL should have a method that receive whatever parameters necessary and create the database call with such parameters.Adhern
Then what is the use of BLL here. I could have call function of DAL directly from Presentation Layer.Hausmann
C
3

You should have something like this:

The UI calls BL.SaveUsernameAndPassword(string user, string pass);

BL.SaveUsernameAndPassword should validate the strings, and then call DAL.SaveUsernameAndPassword(string user, string pass);

DAL.SaveUsernameAndPassword should put these parameters into your SQL query and execute it, with the assumption that the data is valid

Consensual answered 22/1, 2010 at 16:50 Comment(5)
so in this case I can't have a common DAL for all my projects, as I have to write query in DAL.Hausmann
Why does having to write the query in the DAL prevent you from having a common DAL for your projects?Consensual
In some projects I may want to select different number of variables from different tables.Hausmann
Hmm, in that case then you should have a different DAL per project. The BL would then choose which DAL to use, depending on the project.Consensual
Your DAL should literally be a direct map onto your database. You don't want it to contain any logic, or do anything clever like map onto different databases, it just conveys data back and forth.Consensual

© 2022 - 2024 — McMap. All rights reserved.