Layer Model Programming
For programmers....

The following coding is use for .net programmers I have written the coding based on ASP.net ,but any one can use that coding. The the following things are important for that programme,

VB.net Environment

SQL Server2000 specially Stored Procedure knowledge.

 

Customers’ ASP.NET WEB PAGE form back up coding or Customers.ASPX file

#Region "Namespace"

Imports Data.Customers

Imports DAL.Customers

Imports DataColumns.Customers

Imports BLL.Customer

Imports Data.Result

#End Region

 Partial Class Customers

                Inherits System.Web.UI.Page

 

#Region "UI Events"

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

Try

Dim resultObj As New Result  Create the instance/ instanciate for Result class

Dim CustomerData As New CustomerDATA ‘instanciate  the Customer DATA Class

If  Validatemembers() = True Then Exit Sub ‘Validate the Customers input data

binddata(CustomerData) ‘Bind the Customers

resultObj = New CustomerBLL().SaveEvents(CustomerData) ‘Pass the Customer data instance to the CustomersBLL.Savemethod and that function return  the result instance.

 If resultObj.Success Then

           ErrorLabel.Text = "Saved successfully"

 Else

 If resultObj.ResultId = String.Empty Then

           ErrorLabel.Text = "Unknown error. Please contact the administrator"

 Else

           ErrorLabel.Text = resultObj.ResultId

 End If

 End If

  Catch ex As Exception

          ErrorLabel.Text = ex.Message

 End Try

 End Sub

#End Region

 

 #Region "Private methods"

 Private Sub binddata(ByVal customerdata As CustomerDATA) ‘This is used to bind

        customerdata.CustomerName = txtCustomerName.Text

        customerdata.Customer_Addresss = txtCustomerAddress.Text

        customerdata.Country = txtCountry.Text

        customerdata.City = txtCity.Text

        customerdata.Hphone = txtHPhone.Text

        customerdata.LPhone = txtLphone.Text

End Sub

 Private Function Validatemembers() As Boolean ‘Used to Validate the user  input

        Dim errorString As New StringBuilder

        If (txtCustomerName.Text = "") Then

            errorString.Append("You have to enter the Customer Name")

        End If

        If (txtCustomerAddress.Text = "") Then

            errorString.Append(" You have to enter the Customer Address")

        End If

        If (txtCity.Text = "") Then

            errorString.Append("You have to fill the city name")

        End If

        If (txtCountry.Text = "") Then

            errorString.Append("You have to fill the Countery name")

        End If

        If errorString.ToString = String.Empty Then

            Return False

        Else

            ErrorLabel.Text = errorString.ToString

            Return True

       End If

End Function

 #End Region

End Class

 

DATA Layer:

This is used to get and Set the relevant data field members.

 

Customer Data Class

 

Class Customer

#Region "NameSpace"

Imports Microsoft.VisualBasic

#End Region

 

#Region "Modification Log"

'''' <summary>

'''' Date                  Created By

'''' 29/04/2008        Theva

'''' </summary>

#End Region

 

#Region "Data implemetation for the Customer details"

 Namespace Data.Customers

 Public Class CustomerDATA

 

        Private customer_Id As String

        Private customer_Name As String

        Private customer_Address As String

        Private _city As String

        Private _country As String

        Private customer_HPhone As Integer

        Private customer_LPhone As Integer

 

Public Property CustomerID() As String

           Get

                Return customer_Id

            End Get

            Set(ByVal value As String)

                customer_Id = value

            End Set

End Property

Public Property CustomerName() As String

           Get

                Return customer_Name

            End Get

            Set(ByVal value As String)

                customer_Name = value

            End Set

 End Property

 Public Property Customer_Addresss() As String

            Get

                Return customer_Address

            End Get

            Set(ByVal value As String)

                customer_Address = value

            End Set

  End Property

Public Property City() As String

            Get

                Return _city

            End Get

            Set(ByVal value As String)

                _city = value

            End Set

    End Property

    Public Property Country() As String

            Get

                Return _country

            End Get

            Set(ByVal value As String)

                _country = value

            End Set

   End Property

   Public Property Hphone() As Integer

            Get

                Return customer_HPhone

            End Get

            Set(ByVal value As Integer)

                customer_HPhone = value

            End Set

End Property

Public Property LPhone() As Integer

            Get

                Return customer_LPhone

            End Get

            Set(ByVal value As Integer)

                customer_LPhone = value

            End Set

End Property

End Class

End Namespace

#End Region

 

Structure Result Data

 
Imports Microsoft.VisualBasic
 

Namespace Data.Result

<Serializable()> Public Structure Result

 

        Private _success As Boolean

        Private _resultId As String

        Private _result As Object

Sub New(ByVal success As Boolean, ByVal resultID As String, Optional ByVal    result As Object = Nothing)

      _success = success

      _resultId = resultID

      _result = result

End Sub

Property Success() As Boolean

      Get

         Return _success

       End Get

       Set(ByVal Value As Boolean)

           _success = Value

       End Set

End Property

Property ResultId() As String

      Get

        Return _resultId

      End Get

      Set(ByVal Value As String)

        _resultId = Value

      End Set

End Property

Property Result() As Object

      Get

        Return _result

         End Get

         Set(ByVal Value As Object)

          _result = Value

         End Set

End Property

End Structure

End Namespace

 

DATA COLUMNS

Here we will declare the Data Field members.

 

CUSTOMER DATA COLUMNS

-------------------------------------------------------------------------------------------------------------------------------------

#Region "NameSpaces"

Imports Microsoft.VisualBasic

#End Region

 

#Region "Modification Log"

'''' <summary>

'''' 'Date               Created By

'''' '25/04/2008     Theva

'''' '</summary>

'''' '<remarks></remarks>

#End Region 

 

#Region "Customer Data Columns decalarations"

     Namespace DataColumns.Customers

      Public Class CustomersColumns

        Public Const CustomersTable As String = "customers"

        Public Const CustomerId As String = "Customer_Id"

        Public Const CustomerName As String = "Customer_Name"

        Public Const CustomerAddress As String = "Customer_Address"

        Public Const City As String = "City"

        Public Const Country As String = "Country"

        Public Const CustomerHPhone As String = "Customer_HPhone"

        Public Const CustomerLPhone As String = "Customer_LPhone"

    End Class

  End Namespace

#End Region

 

ENUMERATION

 

This is used in the stored procedure for storing and editing the data.

#Region "Namespace"

Imports Microsoft.VisualBasic

#End Region

Namespace Enums.Common

    Public Enum InvokeMode

        imCreate

        imModify

        imUpdate

    End Enum

End Namespace

  

Business Logic Layer (BLL)

This is the layer used to do activities between the Web application form and Data application layer.

Commonly we are writing the business logics in that field.

 

Customer BLL

#Region "Namespaces"

Imports Microsoft.VisualBasic

Imports System.Data

Imports System.Data.SqlClient

 

Imports Data.Customers

Imports Data.Result

Imports DAL.Customers

Imports DataColumns.Customers

Imports Enums.Common

#End Region

 

Namespace BLL.Customer

    Public Class CustomerBLL

    Public Function SaveEvents(ByVal cdata As CustomerDATA)

            Dim invokeMode As InvokeMode

            Try

             If cdata.CustomerID = String.Empty Then

                 invokeMode = invokeMode.imCreate

                  cdata.CustomerID = New CustomersDAL().nextCustomerCode

              If cdata.CustomerID = String.Empty Then

                    Return New Result(False, "An error occured while creating the new users")

              End If

              Else

                    invokeMode = invokeMode.imModify

              End If

              If New CustomersDAL().saveCustomer(cdata, invokeMode) Then

                    Return New Result(True, "Saved successfully")

              Else

                   Return New Result(False, "An error occured while in saving event details")

              End If

            Catch ex As Exception

                Return New Result(False, "Error: " + ex.Message)

            End Try

        End Function

    End Class

End Namespace

 Data Application Layer

--------------------------------------------------------------------------------------------------------------

Via this layer we can send the data and receive the data from Data Base.

This is the only layer which has the direct connection between the data base.

 

 CUTOMERS DAL

-------------------------------------------------------------------------------------------------------------------------------------

#Region "Namespaces"

Imports Microsoft.VisualBasic

Imports System.Data.SqlClient

Imports System.Data

Imports Microsoft.ApplicationBlocks.Data

Imports Common.Security

Imports DataColumns.Customers

Imports Data.Customers

Imports Common.Functions.CommonFunctions

Imports Common.ApplicationConstants

Imports Enums.Common

#End Region

 

Namespace DAL.Customers

     Public Class CustomersDAL

 

Public Function SaveCustomer(ByVal customer As CustomerDATA, ByVal Invokdemode As InvokeMode) As Boolean

Dim param(7) As SqlParameter

 

 param(0) = New SqlParameter("@Customer_Id", customer.CustomerID)

 param(1) = New SqlParameter("@Customer_Name", customer.CustomerName)

 param(2) = New SqlParameter("@Customer_Address", customer.Customer_Addresss)

 param(3) = New SqlParameter("@City", customer.City)

 param(4) = New SqlParameter("@Country", customer.Country)

 param(5) = New SqlParameter("@Customer_HPhone", customer.Hphone)

 param(6) = New SqlParameter("@Customer_LPhone", customer.LPhone)

 param(7) = New SqlParameter("@tInvokeMode", Invokdemode)

 If SqlHelper.ExecuteNonQuery(DecryptDALConnection(ConfigurationManager.AppSettings(AppConfigKeys.DALConnection)), CommandType.StoredProcedure, "StoreCustomer", param) Then

 Return True

 Else

 Return False

 End If

 End Function

 

Public Function GetCustomerDetails(ByVal cdata As CustomerDATA) As DataSet

Dim arParams(0) As SqlParameter

arParams(0) = New SqlParameter("@cCustomerCode", cdata.CustomerID)

Dim dsUserDetails As DataSet = SqlHelper.ExecuteDataset(DecryptDALConnection(ConfigurationManager.AppSettings(AppConfigKeys.DALConnection)), CommandType.StoredProcedure, "SP_GetCustomer", arParams)

       

With dsUserDetails.Tables(0)

        .TableName = CustomersColumns.CustomersTable

        .PrimaryKey = New DataColumn() {.Columns(CustomersColumns.CustomerId)}

End With

 

End Function

 

Public Function nextCustomerCode() As String

 

 Dim abparameter(0) As SqlParameter

 abparameter(0) = New SqlParameter("@cCustomerCode", SqlDbType.Char, 6)

 abparameter(0).Direction = ParameterDirection.Output

            SqlHelper.ExecuteNonQuery(DecryptDALConnection(ConfigurationManager.AppSettings(AppConfigKeys.DALConnection)), CommandType.StoredProcedure, "Esp_Customer_NextCode", abparameter)

 

 If Convert.ToString(abparameter(0).Value) <> String.Empty Then

            Return Convert.ToString(abparameter(0).Value).Trim

 Else

           Return String.Empty

 End If

 

End Function

 End Class

 End Namespace

 --------------------------------------------------------------------------------------------------------------

Common Layer

Here we can place the common things.

Such as Security, Function, Constants…etc

 If we consider the Security,

 

 SECURITY

------------------

#Region "Namespaces"

 Imports Microsoft.VisualBasic

 Imports System.Security.Cryptography

 Imports System.Text

 Imports System.IO

 #End Region

 

 Namespace Common.Security

  Public Class Securityinfo

   #Region " Private Constatants "

       Private Const SALTLENTH As Integer = 4

       Private Const KEY_ID As String = "!@#$%^&*()AM826Gwkf58e?s"

       Private Const VECTOR_ID As String = "w48*+-36dfghjklo"

  #End Region

 

Public Shared Function Encrypt(ByVal PlainText As String) As String

 Dim memStreamEncryptedData As New MemoryStream

   Dim rij As Rijndael

   rij = New RijndaelManaged

   rij.Mode = CipherMode.CBC

   rij.Key = Encoding.ASCII.GetBytes(KEY_ID)

   rij.IV = Encoding.ASCII.GetBytes(VECTOR_ID)

   Dim bytesData As Byte() = Encoding.ASCII.GetBytes(PlainText)

   Dim transform As ICryptoTransform = rij.CreateEncryptor()

   Dim encStream As New CryptoStream(memStreamEncryptedData, transform, CryptoStreamMode.Write)

    Try

         encStream.Write(bytesData, 0, bytesData.Length)

    Catch ex As Exception

     Throw New Exception("Error while writing encrypted data to the stream:" & vbCrLf & ex.Message)

     End Try

    encStream.FlushFinalBlock()

    encStream.Close()

    Return Convert.ToBase64String(memStreamEncryptedData.ToArray())

  End Function

 

   Public Shared Function Decrypt(ByVal cipherText As String) As String

    Dim memStreamDecryptedData As New MemoryStream

    Dim rij As Rijndael

    rij = New RijndaelManaged

    rij.Mode = CipherMode.CBC

    Dim transform As ICryptoTransform    =rij.CreateDecryptor(Encoding.ASCII.GetBytes(KEY_ID), Encoding.ASCII.GetBytes(VECTOR_ID))

    Dim decStream As New CryptoStream(memStreamDecryptedData, transform, CryptoStreamMode.Write)

    Dim bytesData As Byte() = Convert.FromBase64String(cipherText)

     Try

       decStream.Write(bytesData, 0, bytesData.Length)

     Catch ex As Exception

       Throw New Exception("Error while writing encrypted data to the stream:" &  vbCrLf & ex.Message)

     End Try

       decStream.FlushFinalBlock()

       decStream.Close()

      Return Encoding.ASCII.GetString(memStreamDecryptedData.ToArray())

 End Function

 End Class

 End Namespace

 

Common Functions

 

#Region "Namespace"

Imports Microsoft.VisualBasic

Imports Common.Security.securityinfo

Imports System.Configuration

Imports System.Web.Configuration

#End Region

Namespace Common.Functions

  Public Class CommonFunctions

Public Shared Function DecryptDALConnection(ByVal DALConnection As String) As String

     Return Decrypt(DALConnection)

End Function

End Class

End Namespace

Home