Wednesday, October 18, 2006

Developing 3-Tier Application in .NET 2.0 – 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.

1 comment:

Anonymous said...

Nice to see another Vishwa Mohan that too from Gaya. Well, I am Vishwa Mohan from Bhagalpur infact kept hopping from Bihar to Jhrakkand so not sure from which state I belong to. I was looking for vishwamohan domain and then tried to find out in blogspot that too you have taken. Not an issue will get some domain but was really nice to meet another Vishwa Mohan as earlier I came across only with Vishwa Mohan Bhatt.
Nice job keep it up.