3 Tier Application Architecture, logic and database

(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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật