Wednesday, May 16, 2012

How to work on 3-Layer architecture in .NET


                                                   
When we work on Layered architecture we follow some techniques to do work on it.
The main purpose of this technique is for security purpose.

                                                                Layer -1
                                                       ==============

METHOD FOR DAL(Date Access Layer) :
=================================
Here we have to define number of methods related to Database access.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Net;
using System.Net.Mail;

/// <summary>
/// Summary description for InvoiceDAL
/// </summary>
public class InvoiceDAL : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader dr;
    DataTable dt;
    public static int i, j, k;
    public string[] arr = new string[1000];
    public InvoiceDAL()
    {
        con = new SqlConnection(HttpContext.Current.Session["connection"].ToString());     //Connection             defined on web.config File.
    }
    public int insert(string invoiceId, string Subject, int SalesOrderId, int AccountId,
                      string CustomerName, string PhoneNo)
    {
        con.Open();
        cmd = new SqlCommand("select Id_int from Base_SalesOrder", con);
        dr = cmd.ExecuteReader();
        dr.Read();
        i = Convert.ToInt32(dr[0].ToString());
        Session["SalesOrderId"] = i;
        dr.Close();
        con.Close();
        con.Open();

        cmd = new SqlCommand("select [Id_int] from Base_PurchaseOrder", con);
        dr = cmd.ExecuteReader();
        dr.Read();
        k = Convert.ToInt32(dr[0].ToString());
        Session["PurchaseOrderId"] = k;
        dr.Close();
        con.Close();
        con.Open();
        SqlCommand Cmd = new SqlCommand("insert_invoice6", con);
        Cmd.CommandType = CommandType.StoredProcedure;

        try
        {
            Cmd.Parameters.AddWithValue("@InvoiceId_vc", invoiceId);
            Cmd.Parameters.AddWithValue("@Subject_vc", Subject);
            Cmd.Parameters.AddWithValue("@SalesOrderId_int", i);
            Cmd.Parameters.AddWithValue("@AccountId_int", j);
            Cmd.Parameters.AddWithValue("@CustomerName_vc", CustomerName);
            Cmd.Parameters.AddWithValue("@CompanyName_vc", CompanyName);
            Cmd.Parameters.AddWithValue("@PhoneNo_vc", PhoneNo);
            return Cmd.ExecuteNonQuery();

        }

        catch
        {

            throw;

        }

        finally
        {



            Cmd.Dispose();

            con.Close();

            con.Dispose();



        }
    }
}


                                                                   Layer-2
                                                            ===========
METHOD FOR BAL(Business  Access Layer) :
=================================

Here, we define a object of DAL and access the DAL's Method.
BAL works as a intermedeate layer and used for the security purpose.
The code is here..


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;

/// <summary>
/// Summary description for invoiceBAL
/// </summary>
public class invoiceBAL : System.Web.UI.Page
{
    InvoiceDAL objDAL = new InvoiceDAL();

    public invoiceBAL()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public int insert(string invoiceId, string Subject, int i, int j,
        string CustomerName, string PhoneNo)
    {
        try
        {

            return objDAL.insert(invoiceId, Subject, i, j, CustomerName, PhoneNo);
        }
        catch
        {

            throw;

        }

        finally
        {

            objDAL = null;

        }

    }
}

                                                             Layer-3
                                                    ============
Method for .CS Page:-
=================
This is the final layer i.e. . CS file.
This is same as the BAL. we only need to create an object of BAL Class and just call the method which defined on BAL.
The  code is ..


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web.UI.WebControls.WebParts;
using System.Web.Security;


public partial class Invoice : ThemeClass
{

 protected void Page_Load(object sender, EventArgs e)
    {

            InvoiceId = lblInvoiceID.Text;
            string Subject = txtsubject.Text;
            if (Button1.Text == "Submit")     //Check the button text i.e. SUBMIT or NOT.
            {
                invoiceBAL objBAL = new invoiceBAL();
                int i = Convert.ToInt32(Session["SalesOrderId"]);
                string CustomerName = txtcustomername.Text;
                string CompanyName = txtcompanyname.Text;
                string PhoneNo = txtphonenumber.Text;
           
                try
                {
                    intResult = objBAL.insert(InvoiceId, Subject, i, j, CustomerName, PhoneNo);
                }
                finally
                {
                    objBAL = null;
                }

                if (Button1.Text == "Submit")
                {
                    InsertData();
                }

            }




No comments:

Post a Comment