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 CustomerPrivate _custID As Integer = 0Private _custName As String = String.EmptyPrivate _custDOB As DateTime = DateTime.MinValuePrivate _custAddress As String = String.EmptyPrivate _dateCreated As DateTime = DateTime.NowPrivate _dateModified As DateTime = DateTime.Now#Region "Constructor"Public Sub New()End Sub#End Region
#Region "Properties"Public Property CustID() As IntegerGet
Return _custID
End GetSet(ByVal value As Int32)_custID = valueEnd SetEnd PropertyPublic Property CustName() As StringGet
Return _custName
End GetSet(ByVal value As String)_custName = valueEnd SetEnd PropertyPublic Property CustDOB() As DateTimeGet
Return _custDOB
End GetSet(ByVal value As DateTime)_custDOB = valueEnd SetEnd PropertyPublic Property CustAddress() As StringGet
Return _custAddress
End GetSet(ByVal value As String)_custAddress = valueEnd SetEnd PropertyPublic Property DateCreated() As DateTimeGet
Return _dateCreated
End GetSet(ByVal value As DateTime)_dateCreated = valueEnd SetEnd PropertyPublic Property DateModified() As DateTimeGet
Return _dateModified
End GetSet(ByVal value As DateTime)_dateModified = valueEnd SetEnd Property#End Region
#Region "--Customer Object Functions--- "Public Shared Function GetCustomer(ByVal custID As Integer) As CustomerReturn DataAccess.GetCustomers(custID)
End FunctionPublic Shared Function GetAllCustomer() As Generic.List(Of Customer)Return DataAccess.GetAllCustomers()
End FunctionPublic Shared Function AddCustomer() As IntegerDim custInfo As New CustomercustInfo.CustID = 0custInfo.CustName = "unknown"
custInfo.CustDOB = #1/1/1900#custInfo.CustAddress = "unknown"
Return DataAccess.InsertCustomer(custInfo)
End FunctionPublic Shared Function AddCustomer(ByVal custInfo As Customer) As IntegerReturn DataAccess.InsertCustomer(custInfo)
End FunctionPublic Shared Function UpdateCustomer(ByVal custInfo As Customer) As IntegerReturn DataAccess.UpdateCustomer(custInfo)
End FunctionPublic Shared Function DeleteCustomer(ByVal custInfo As Customer) As IntegerReturn DataAccess.DeleteCustomer(custInfo)
End Function#End Region
End ClassEnd 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
- <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Customer.aspx.vb" Inherits="Vishwa.Example.WebSite1.Customer" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head id="Head1" runat="server" >
- <title>Customer</title>
- </head>
- <body>
- <form id="frmCustomer" runat="server">
- <div style="text-align:center">
- <h2 >Customer Maintenance</h2>
- <asp:LinkButton ID="lnkAddNew" runat="server" Text="Add New" OnClick="InsertBlankRecord" EnableViewState="false" />
- <asp:GridView ID="gdvCustomer" DataKeyNames="CustID" runat="server" AllowPaging="True" DataSourceID="odsCustomer" >
- <Columns>
- <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
- </Columns>
- <EmptyDataTemplate>
- No Customer Record Found.
- </EmptyDataTemplate>
- </asp:GridView>
- </div>
- <asp:ObjectDataSource ID="odsCustomer" runat="server" DataObjectTypeName="Vishwa.Example.Business.Customer"
- DeleteMethod="DeleteCustomer" SelectMethod="GetAllCustomer"
- TypeName="Vishwa.Example.Business.Customer" UpdateMethod="UpdateCustomer">
- </asp:ObjectDataSource>
- </form>
- </body>
- </html>
--------------------------------------------------------------------------
Code File: Customer.Aspx.vb
--------------------------------------------------------------------------Option Explicit OnOption 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 SubEnd ClassEnd NamespaceCode 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 addedCase 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.
Wednesday, October 18, 2006
Developing 3-Tier Application in .NET 2.0 – Part 3
Subscribe to:
Post Comments (Atom)
1 comment:
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.
Post a Comment