I will be consuming the data which was returned by Data Access Layer in the form of XML, Dataset, DataTable, ArrayList and Custom (Data Transfer) Objects to populating my Business Object, which will be finally consumed by a User Interface and most likely a web page.
In this example, I will use only one class, which will perform the CRUD operation on Customer Object by invoking method of Data Access Layer. Since I have added methods in data access layer to return Customer(s) data in form on XML, Dataset, DataTable, ArrayList and Custom (Data Transfer) Object. No matter in which form data is returned from data access layer, the business object will read that object and consume the data to make it ready for business object caller.
This Business Object will refer the assembly I previously created Vishwa.Example.Data in my earlier post Data Transfer Objects (DTO) - Data Access Layer .
Following are the stepsCreated a Class Library Project in Visual Studio 2005 using language VB.NETProject Name: Vishwa.Example.BusinessClass Name: CustomerBiz.vbCustomerBiz.vb
' <summary>' Customer Business Object' </summary>' <remarks>' Design Pattern: Domain Model and Identity Field' </remarks>Option Explicit OnOption Strict OnImports Vishwa.Example.DataImports Vishwa.Example.Data.DataAccessNamespace Example.Business<Serializable()> _Public Class CustomerBizPrivate _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 IntegerGetReturn _custIDEnd GetSet(ByVal value As Int32)_custID = valueEnd SetEnd PropertyPublic Property CustName() As StringGetReturn _custNameEnd GetSet(ByVal value As String)_custName = valueEnd SetEnd PropertyPublic Property CustDOB() As DateTimeGetReturn _custDOBEnd GetSet(ByVal value As DateTime)_custDOB = valueEnd SetEnd PropertyPublic Property CustAddress() As StringGetReturn _custAddressEnd GetSet(ByVal value As String)_custAddress = valueEnd SetEnd PropertyPublic Property DateCreated() As DateTimeGetReturn _dateCreatedEnd GetSet(ByVal value As DateTime)_dateCreated = valueEnd SetEnd PropertyPublic Property DateModified() As DateTimeGetReturn _dateModifiedEnd GetSet(ByVal value As DateTime)_dateModified = valueEnd SetEnd Property' A Simple example of Business Logic to Get Customer Age' Such logics are created in Business Logic/Object LayerPublic ReadOnly Property CustAge() As LongGetIf _custDOB > DateTime.MinValue ThenReturn DateDiff(DateInterval.Year, _custDOB, Now())ElseReturn 0End IfEnd GetEnd Property#End Region#Region "Customer Methods"Public Function AddBlankCustomer() As IntegerReturn DataProvider.InsertCustomer("unknown", DateTime.Now, "Unknown")End FunctionPublic Overloads Function AddCustomer(ByVal custInfo As CustomerBiz) As IntegerReturn DataProvider.InsertCustomer(custInfo.CustName, custInfo.CustDOB, "Unknown")End FunctionPublic Overloads Function AddCustomer(ByVal custName As String, ByVal custDOB As DateTime, _ByVal custAddress As String) As IntegerReturn DataProvider.InsertCustomer(custName, custDOB, custAddress)End FunctionPublic Overloads Function UpdateCustomer(ByVal custInfo As CustomerBiz) As BooleanReturn DataProvider.UpdateCustomer(custInfo.CustID, custInfo.CustName, custInfo.CustDOB, custInfo.CustAddress)End FunctionPublic Overloads Function UpdateCustomer(ByVal custID As Integer, ByVal custName As String, _ByVal custDOB As DateTime, ByVal custAddress As String) As BooleanReturn DataProvider.UpdateCustomer(custID, custName, custDOB, custAddress)End FunctionPublic Overloads Function DeleteCustomer(ByVal custID As Integer) As BooleanReturn DataProvider.DeleteCustomer(custID)End FunctionPublic Overloads Function DeleteCustomer(ByVal custInfo As CustomerBiz) As BooleanReturn DataProvider.DeleteCustomer(custInfo.CustID)End Function#Region "Get Customers From Custom Data Transfer Object"Public Function GetCustomer(ByVal custID As Integer) As CustomerBizDim custDTORec As New CustomerDTODim custBIZRec As New CustomerBizcustDTORec = DataProvider.GetCustomer(custID)If Not custDTORec Is Nothing ThencustBIZRec = GetCustomerRow(custDTORec)End IfReturn custBIZRecEnd FunctionPublic Function GetCustomers() As List(Of CustomerBiz)Dim custBIZRecs As New List(Of CustomerBiz)Dim custDTORecs As New List(Of CustomerDTO)custDTORecs = DataProvider.GetCustomersFor Each custDTORec As CustomerDTO In custDTORecsDim custBIZRec As CustomerBiz = GetCustomerRow(custDTORec)custBIZRecs.Add(custBIZRec)NextReturn custBIZRecsEnd Function#End Region#Region "Consuming Get Methods of Other Data Trasfer Objects"#Region "Get Customers From DataSet Object"Public Function GetCustomerFromDataSet(ByVal custID As Integer) As CustomerBizDim custDataSet As New DataSet("Customer")Dim custBIZRec As New CustomerBizcustDataSet = DataProvider.GetCustomerAsDataSet(custID)If Not custDataSet Is Nothing AndAlso custDataSet.Tables.Count = 1 ThencustBIZRec = GetCustomerRow(custDataSet.Tables(0).Rows(0))End IfReturn custBIZRecEnd FunctionPublic Function GetCustomersFromDataSet() As List(Of CustomerBiz)Dim custDataSet As New DataSet("Customers")Dim custBIZRecs As New List(Of CustomerBiz)custDataSet = DataProvider.GetCustomersAsDataSet()If Not custDataSet Is Nothing AndAlso custDataSet.Tables.Count = 1 ThenFor Each row As DataRow In custDataSet.Tables(0).RowsDim custBIZRec As CustomerBiz = GetCustomerRow(row)custBIZRecs.Add(custBIZRec)NextEnd IfReturn custBIZRecsEnd Function#End Region#Region "Get Customers From DataTable Object"Public Function GetCustomerFromDataTable(ByVal custID As Integer) As CustomerBizDim custDataTable As New DataTable("Customer")Dim custBIZRec As New CustomerBizcustDataTable = DataProvider.GetCustomerAsDataTable(custID)If Not custDataTable Is Nothing AndAlso custDataTable.Rows.Count > 0 ThencustBIZRec = GetCustomerRow(custDataTable.Rows(0))End IfReturn custBIZRecEnd FunctionPublic Function GetCustomersFromDataTable() As List(Of CustomerBiz)Dim custDataTable As New DataTable("Customers")Dim custBIZRecs As New List(Of CustomerBiz)custDataTable = DataProvider.GetCustomersAsDataTable()If Not custDataTable Is Nothing AndAlso custDataTable.Rows.Count > 0 ThenFor Each row As DataRow In custDataTable.RowsDim custBIZRec As CustomerBiz = GetCustomerRow(row)custBIZRecs.Add(custBIZRec)NextEnd IfReturn custBIZRecsEnd Function#End Region#Region "Get Customers From XML"Public Function GetCustomerFromXML(ByVal custID As Integer) As CustomerBizDim custBIZRecs As New List(Of CustomerBiz)Dim custBIZRec As New CustomerBizDim custXMLString As String = DataProvider.GetCustomersAsXMLString()If custXMLString.Length > 0 ThencustBIZRecs = ReadCustomerXML(custXMLString)If Not custBIZRecs.Count > 0 Then custBIZRec = custBIZRecs(0)End IfReturn custBIZRecEnd FunctionPublic Function GetCustomersFromXML() As List(Of CustomerBiz)Dim custBIZRecs As New List(Of CustomerBiz)Dim custXMLString As String = DataProvider.GetCustomersAsXMLString()If custXMLString.Length > 0 ThencustBIZRecs = ReadCustomerXML(custXMLString)End IfReturn custBIZRecsEnd Function#End Region#Region "Get Customers From ArrayList Object"Public Function GetCustomerFromArrayList(ByVal custID As Integer) As CustomerBizDim custBIZRec As New CustomerBizDim custArrList As New ArrayList()custArrList = DataProvider.GetCustomerAsArrayList(custID)If Not custArrList Is Nothing AndAlso custArrList.Count > 0 ThencustBIZRec = GetCustomerRow(custArrList)End IfReturn custBIZRecEnd FunctionPublic Function GetCustomersFromArrayList() As List(Of CustomerBiz)Dim custBIZRecs As New List(Of CustomerBiz)Dim custArrList As New ArrayList()custArrList = DataProvider.GetCustomersAsArrayList()If Not custArrList Is Nothing AndAlso custArrList.Count > 0 ThenFor Each row As ArrayList In custArrListDim custBIZRec As CustomerBiz = GetCustomerRow(row)custBIZRecs.Add(custBIZRec)NextEnd IfReturn custBIZRecsEnd Function#End Region#End Region#Region "Common Methods to Transfer data from DTO to Business Object"Private Function GetCustomerRow(ByVal row As DataRow) As CustomerBizDim custBIZRec As New CustomerBizIf Not IsDBNull(row.Item("Cust_ID")) Then custBIZRec.CustID = CInt(row.Item("Cust_ID"))If Not IsDBNull(row.Item("Cust_DOB")) Then custBIZRec.CustDOB = CDate(row.Item("Cust_DOB"))If Not IsDBNull(row.Item("Cust_Name")) Then custBIZRec.CustName = row.Item("Cust_Name").ToStringIf Not IsDBNull(row.Item("Cust_Address")) Then custBIZRec.CustAddress = row.Item("Cust_Address").ToStringIf Not IsDBNull(row.Item("Date_Created")) Then custBIZRec.DateCreated = CDate(row.Item("Date_Created"))If Not IsDBNull(row.Item("Date_Modified")) Then custBIZRec.DateModified = CDate(row.Item("Date_Modified"))Return custBIZRecEnd FunctionPrivate Function GetCustomerRow(ByVal custDTORec As CustomerDTO) As CustomerBizDim custBIZRec As New CustomerBizcustBIZRec.CustID = custDTORec.CustIDcustBIZRec.CustName = custDTORec.CustNamecustBIZRec.CustDOB = custDTORec.CustDOBcustBIZRec.CustAddress = custDTORec.CustAddresscustBIZRec.DateCreated = custDTORec.DateCreatedcustBIZRec.DateModified = custDTORec.DateModifiedReturn custBIZRecEnd FunctionPrivate Function GetCustomerRow(ByVal custArrList As ArrayList) As CustomerBizDim custBIZRec As New CustomerBizIf IsNumeric(custArrList.Item(0)) Then custBIZRec.CustID = CInt(custArrList.Item(0))If Not custArrList.Item(1) Is Nothing Then custBIZRec.CustName = custArrList.Item(1).ToStringIf IsDate(custArrList.Item(2)) Then custBIZRec.CustDOB = CDate(custArrList.Item(2))If Not custArrList.Item(3) Is Nothing Then custBIZRec.CustAddress = custArrList.Item(3).ToStringIf IsDate(custArrList.Item(4)) Then custBIZRec.DateCreated = CDate(custArrList.Item(4))If IsDate(custArrList.Item(5)) Then custBIZRec.DateModified = CDate(custArrList.Item(5))Return custBIZRecEnd FunctionPrivate Function ReadCustomerXML(ByVal xmlDocString As String) As List(Of CustomerBiz)Dim customers As New List(Of CustomerBiz)Dim status As Integer = 0Dim statusDescription As String = String.EmptyDim uniEncoding As New System.Text.ASCIIEncoding()Dim stringBytes As Byte() = uniEncoding.GetBytes(xmlDocString)Dim xmlDocSteam As New System.IO.MemoryStream(stringBytes, 0, stringBytes.Length)Using xmlDocReader As System.Xml.XmlReader = System.Xml.XmlReader.Create(xmlDocSteam)xmlDocReader.Read()xmlDocReader.MoveToContent()While xmlDocReader.Read()If xmlDocReader.NodeType = System.Xml.XmlNodeType.Element And xmlDocReader.Name = "Customer" ThenDim custRec As New CustomerBizWhile (xmlDocReader.Read() And xmlDocReader.Name <> "Customer")If xmlDocReader.Name.Length > 0 ThenIf xmlDocReader.Name = "Cust_ID" Then custRec.CustID = xmlDocReader.ReadElementContentAsInt("Cust_ID", "")If xmlDocReader.Name = "Cust_Name" Then custRec.CustName = xmlDocReader.ReadElementContentAsString("Cust_Name", "")If xmlDocReader.Name = "Cust_DOB" Then custRec.CustDOB = xmlDocReader.ReadElementContentAsDateTime("Cust_DOB", "")If xmlDocReader.Name = "Cust_Address" Then custRec.CustAddress = xmlDocReader.ReadElementContentAsString("Cust_Address", "")If xmlDocReader.Name = "Date_Created" Then custRec.DateCreated = xmlDocReader.ReadElementContentAsDateTime("Date_Created", "")If xmlDocReader.Name = "Date_Modified" Then custRec.DateModified = xmlDocReader.ReadElementContentAsDateTime("Date_Modified", "")End IfEnd Whilecustomers.Add(custRec)End IfEnd WhileEnd UsingxmlDocSteam.Close()Return customersEnd Function#End Region#End RegionEnd ClassEnd NamespaceCompiled Assembly Name: Vishwa.Example.Business.dllNow you can use the reference of the above assembly for customer object in your ASP.NET Web Page and test the functionality.
No comments:
Post a Comment