Vishwamohan

Welcome to Vishwa's blog - Technology, Spirituality and More...

Developing 3 Tier Application in .NET - Part 3

In this section, I will connect Business Logic Layer (BLL) to Data Access Layer and then User Interface Layer (UIL) to Business Logic Layer (BLL).

Note: If you are developing an enterprise application please see my next article on “Understanding 3-Tier vs. 3-Layer Architecture”.

Business Logic Layer Code
Namespace:
Vishwa.Example.Business

Customer.vb : This class will communicate with previously designed Data Access Layer (DAL) in Part -2.

 
' Author : Vishwa
 
' Date : 10/15/2006
 
' Class : Customer Business Object
 
' Design Pattern: Domain Model and Identity Field
 
' Purpose: This class will act as Business Object and Business Logic Layer
 
 
Imports Vishwa.Example.Data
 
 
 
Namespace Vishwa.Example.Business
 
    Public Class Customer
 
 
 
        Private _custID As Integer = 0
 
        Private _custName As String = String.Empty
 
        Private _custDOB As DateTime = DateTime.MinValue
 
        Private _custAddress As String = String.Empty
 
        Private _dateCreated As DateTime = DateTime.Now
 
        Private _dateModified As DateTime = DateTime.Now
 
 
 
#Region "Constructor"
 
        Public Sub New()
 
 
 
        End Sub
 
#End Region
 
 
 
#Region "Properties"
 
 
 
        Public Property CustID() As Integer
 
            Get
 
                Return _custID
 
            End Get
 
 
 
            Set(ByVal value As Int32)
 
                _custID = value
 
            End Set
 
        End Property
 
 
 
        Public Property CustName() As String
 
            Get
 
                Return _custName
 
            End Get
 
            Set(ByVal value As String)
 
                _custName = value
 
            End Set
 
        End Property
 
 
 
        Public Property CustDOB() As DateTime
 
            Get
 
                Return _custDOB
 
            End Get
 
            Set(ByVal value As DateTime)
 
                _custDOB = value
 
            End Set
 
        End Property
 
 
 
        Public Property CustAddress() As String
 
            Get
 
                Return _custAddress
 
            End Get
 
            Set(ByVal value As String)
 
                _custAddress = value
 
            End Set
 
        End Property
 
 
 
        Public Property DateCreated() As DateTime
 
            Get
 
                Return _dateCreated
 
            End Get
 
            Set(ByVal value As DateTime)
 
                _dateCreated = value
 
            End Set
 
        End Property
 
 
 
        Public Property DateModified() As DateTime
 
            Get
 
                Return _dateModified
 
            End Get
 
            Set(ByVal value As DateTime)
 
                _dateModified = value
 
            End Set
 
        End Property
 
 
 
#End Region
 
 
 
#Region "--Customer Object Functions--- "
 
 
 
        Public Shared Function GetCustomer(ByVal custID As Integer) As Customer
 
            Return DataAccess.GetCustomers(custID)
 
        End Function
 
 
 
        Public Shared Function GetAllCustomer() As Generic.List(Of Customer)
 
            Return DataAccess.GetAllCustomers()
 
        End Function
 
 
 
        Public Shared Function AddCustomer() As Integer
 
            Dim custInfo As New Customer
 
            custInfo.CustID = 0
 
            custInfo.CustName = "unknown"
 
            custInfo.CustDOB = #1/1/1900#
 
            custInfo.CustAddress = "unknown"
 
            Return DataAccess.InsertCustomer(custInfo)
 
        End Function
 
 
 
        Public Shared Function AddCustomer(ByVal custInfo As Customer) As Integer
 
            Return DataAccess.InsertCustomer(custInfo)
 
        End Function
 
 
 
        Public Shared Function UpdateCustomer(ByVal custInfo As Customer) As Integer
 
            Return DataAccess.UpdateCustomer(custInfo)
 
        End Function
 
        Public Shared Function DeleteCustomer(ByVal custInfo As Customer) As Integer
 
            Return DataAccess.DeleteCustomer(custInfo)
 
        End Function
 
#End Region
 
 
 
    End Class
 
End Namespace
 
 
Now, we have Database, Data Access Layer(DAL) and Business Logic Layer (BLL) ready for last step - User Interface Layer (UIL).

User Interface Layer (UIL)
Namespace: Vishwa.Example.WebSite1

Let's add a Web Form in the Project and drop a GridView Control and Object Data Source Control on this page and make sure that you have following code in the Source.
Customer.Aspx
-----------------------------------------------------------------
Code Snippet
  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Customer.aspx.vb" Inherits="Vishwa.Example.WebSite1.Customer" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head id="Head1" runat="server" >
  5. <title>Customer</title>
  6. </head>
  7. <body>
  8. <form id="frmCustomer" runat="server">
  9. <div style="text-align:center">
  10. <h2 >Customer Maintenance</h2>
  11. <asp:LinkButton ID="lnkAddNew" runat="server" Text="Add New" OnClick="InsertBlankRecord" EnableViewState="false" />
  12. <asp:GridView ID="gdvCustomer" DataKeyNames="CustID" runat="server" AllowPaging="True" DataSourceID="odsCustomer" >
  13. <Columns>
  14. <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
  15. </Columns>
  16. <EmptyDataTemplate>
  17. No Customer Record Found.
  18. </EmptyDataTemplate>
  19. </asp:GridView>
  20. </div>
  21. <asp:ObjectDataSource ID="odsCustomer" runat="server" DataObjectTypeName="Vishwa.Example.Business.Customer"
  22. DeleteMethod="DeleteCustomer" SelectMethod="GetAllCustomer"
  23. TypeName="Vishwa.Example.Business.Customer" UpdateMethod="UpdateCustomer">
  24. </asp:ObjectDataSource>
  25. </form>
  26. </body>
  27. </html>


--------------------------------------------------------------------------
Code File: Customer.Aspx.vb
--------------------------------------------------------------------------
Option Explicit On
Option Strict On
 
 
' Author : Vishwa@VishwaMohan.Com
' Date : 10/15/2006
' Class : Customer
' Purpose: To Get,Insert, Update and Delete Customer Record
 
Namespace Vishwa.Example.WebSite1
    Partial Class Customer
        Inherits System.Web.UI.Page
        Public Sub InsertBlankRecord(ByVal sender As Object, ByVal e As System.EventArgs)
            Vishwa.Example.Business.Customer.AddCustomer()
            gdvCustomer.DataBind()
        End Sub
    End Class
End Namespace
 
Code is complete now. Select Customer.aspx page in Project and Press F5 button (run). You will see the customer page as :

Case 1 : No Record.

 

Case 2: Click on Add New Link and a Blank new record with default value will be added
 
Case 3: Click on Edit link and enter a desired value for customer name and address and then click on Update link to update the record.
 
Case 4: Updated record is shown below. You can delete this record by clicking on Delete link. The record will be deleted and you will see the same screen presented in Case 1.

 

I hope this simple example helped you to understand and implement a 3 tier architecture using Visual Studio.NET 2005. If you have any questions or comments, please feel free to post and I will try to answer them.

Once you are able to understand these layer now, let's review the real information in next post.

Comments (5) -

  • Kenneth

    9/26/2008 2:13:19 AM | Reply

    When I try to do as you I get a circular reference dependency error. This happens because the BLL knows DAL And DAL knows BLL. What is your comment to thsi?

  • Vishwa

    9/27/2008 2:56:45 PM | Reply

    I don't think you are doing correctly, because DAL does not know anything about anything about BLL, its a clear seperation. BLL depends on DAL but DAL does not depend on BLL.

  • Kenneth Agerskov

    9/28/2008 7:21:45 AM | Reply

    Your code shows that your DAL returns a BLL object -> [DAL: Public Shared Function GetCustomers(ByVal custID As Integer) As Customer]
    Thus your DAL knows BLL and as BLL instantiates DAL objects there is a circular dependency!  

  • Vishwa

    9/28/2008 8:44:09 AM | Reply

    Ok. Let me point it out here that, you are looking at a Layered approach, you should read the next article www.vishwamohan.com/ShowArticle.aspx?ArticleID=13, hopefully that will clarify your doubts. For a miniue I thought you were talking about another article in which I clearly seperated each layer in its own assembly - www.vishwamohan.com/ShowArticle.aspx?ArticleID=40 and www.vishwamohan.com/ShowArticle.aspx?ArticleID=41 using  DTO.
    In the current article, BO is also acting as data transfer object (DTO) and they all live in same assembly but in a different name space, in this case you will need to have BO fields and properties defined first and then develop DAL and then come back to BO again to create methods, in that way you will not find any circular reference. As everything here is working code. Hope this helps.

Loading