(Note: The examples are simplified)
I’ve seperated my solution into 3 projects
- WebSite
- Business Logic
- Data Access Logic
The Website has access to BL and DAL
The BL has access to nothing
The DAL has access to the BL
On the website I can do something like
BL.User user = DAL.GetUser(1)
I also have this type of logic in the website
user = [new user information]
If Not DAL.DoesEmailExists(user) Then
DAL.InsertNewUser(user)
End If
What would be my option to remove this if/insert from the website?
My ideal option would be to put this logic inside the BL.User but that’s not possible. If the BL has access to the DAL it will make a circular reference.
3
This type of code actually belongs in the Business Layer, from where you control the behavior of your application, make decisions about inserting an object or displaying an alert to the user, etc.,
// BL
If Not DAL.Exists(user) Then
DAL.InsertNewUser(user)
Else
// Maybe warn the user about inserting a duplicate user object
WL.Alert("User already exists!")
End If
You BL should then have access to both the DAL and the WL, and your DAL should not have a dependency on the BL (it only knows how to retrieve data on demand). Do not confuse a Business Object (BO), which can also be a Data Transfer Object (DTO), with a BL: a BO is simply for transferring or passing data from layer to layer.
0
I assume its ASP.NET webforms, go through building layered applications series by Imar Spaanjaars which explains same concept you’ve described. In short, below is the approach.
Presentation Layer
From aspx.cs:
var objCustomer = CustomerBLL.GetCustomerById(id);
if(objCustomer != null)
{
//assign values to UI controls
}
In Business Logic Layer
public static class CustomerBLL
{
public static Customer GetCustomerById(int id)
{
//call database layer
return CustomerDAL.GetCustomerById(id);
}
}
In Data Access Layer
public static class CustomerDAL
{
public static Customer GetCustomerById(int id)
{
Customer objCustomer = new Customer();
//use ADO.NET or any data access methodology to retrieve data
//and assign properties of objCustomer
return objCustomer;
}
}
Business Objects
These are just DTO’s that are used to pass data between layers. It gets referenced in both Business Logic and Data Access Layers.
public class Customer
{
public string Name {get;set;}
//other properties
}
IMO, it works well with small to medium scale applications.
Edit
Depending upon your code, both layers DAL and BLL are getting called from presentation layer. So there is no right answer, unless we know responsibility of each layer and how it is designed.
On the website I can do something like
BL.User user = DAL.GetUser(1)
I also have this type of logic in the website
user = [new user information] If Not DAL.DoesEmailExists(user) Then DAL.InsertNewUser(user) End If
From existing code, as mentioned in the comments section, try this way:
Website
DAL.UpsertUser(objUser);
In store proc:
IF EXISTS(select 1 FROM users where userid = @userid)
BEGIN
--insert
END
ELSE
BEGIN
--update
END
}
It works for both insert/update.
4